请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3353066 新帖:26

量化学习第十六天——python的class使用方法

谎言梦发表于:9 月 25 日 18:00回复(1)

一、类定义

class <类名>:
<语句>

类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性,如果直接使用类名修改其属性,那么将直接影响到已经实例化的对象。

类的私有属性
__private_attrs 两个下划线开头,声明该属性为私有,不能在类地外部被使用或直接访问,在类内部的方法中使用时 self.__private_attrs。

类的方法
在类地内部,使用def关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数self,且为第一个参数。

私有的类方法
__private_method 两个下划线开头,声明该方法为私有方法,不能在类地外部调用,在类的内部调用slef.__private_methods。

类的专有方法
init 构造函数,在生成对象时调用
del 析构函数,释放对象时使用
repr 打印,转换
setitem按照索引赋值
getitem按照索引获取值
len获得长度
cmp比较运算
call函数调用
add加运算
sub减运算
mul乘运算
div除运算
mod求余运算
pow乘方

示例

#类定义  
class people:  
    #定义基本属性  
    name = ''  
    age = 0  
    #定义私有属性,私有属性在类外部无法直接进行访问  
    __weight = 0  
    #定义构造方法  
    def __init__(self,n,a,w):  
        self.name = n  
        self.age = a  
        self.__weight = w  
    def speak(self):  
        print("%s is speaking: I am %d years old" %(self.name,self.age))  

p = people('tom',10,30)  
p.speak()

二、继承类定义
1.单继承
class <类名>(父类名)
<语句>
eg.
class childbook(book)
age = 10

#单继承示例  
class student(people):  
    grade = ''  
    def __init__(self,n,a,w,g):  
        #调用父类的构函  
        people.__init__(self,n,a,w)  
        self.grade = g  
    #覆写父类的方法  
    def speak(self):  
        print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))  

s = student('ken',20,60,3)  
s.speak()

2.类的多重继承

class 类名(父类1,父类2,....,父类n)
<语句1>

需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索,即方法在子类中未找到时,从左到右查找父类中是否包含方法。

**

#另一个类,多重继承之前的准备  
class speaker():  
    topic = ''  
    name = ''  
    def __init__(self,n,t):  
        self.name = n  
        self.topic = t  
    def speak(self):  
        print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))  

#多重继承  
class sample(speaker,student):  
    a =''  
    def __init__(self,n,a,w,g,t):  
        student.__init__(self,n,a,w,g)  
        speaker.__init__(self,n,t)  

test = sample("Tim",25,80,4,"Python")  
test.speak()#方法名同,默认调用的是在括号中排前地父类的方法

**
类方法的覆写——子类覆盖掉父类的方法
def 方法名与父类一致
若是在方法中要使用到父类方法 父类名.方法名

三、若是将类放到了模块中使用时
import A
l = A.类()

#类定义  
class people:  
    #定义基本属性  
    name = ''  
    age = 0  
    #定义私有属性,私有属性在类外部无法直接进行访问  
    __weight = 0  
    #定义构造方法  
    def __init__(self,n,a,w):  
        self.name = n  
        self.age = a  
        self.__weight = w  
    def speak(self):  
        print("%s is speaking: I am %d years old" %(self.name,self.age))  
  
  
p = people('tom',10,30)  
p.speak()  
tom is speaking: I am 10 years old
#单继承示例  
class student(people):  
    grade = ''  
    def __init__(self,n,a,w,g):  
        #调用父类的构函  
        people.__init__(self,n,a,w)  
        self.grade = g  
    #覆写父类的方法  
    def speak(self):  
        print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))  
  
s = student('ken',20,60,3)  
s.speak()  
ken is speaking: I am 20 years old,and I am in grade 3
#另一个类,多重继承之前的准备  
class speaker():  
    topic = ''  
    name = ''  
    def __init__(self,n,t):  
        self.name = n  
        self.topic = t  
    def speak(self):  
        print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))  
  
#多重继承  
class sample(speaker,student):  
    a =''  
    def __init__(self,n,a,w,g,t):  
        student.__init__(self,n,a,w,g)  
        speaker.__init__(self,n,t)  
  
test = sample("Tim",25,80,4,"Python")  
test.speak()#方法名同,默认调用的是在括号中排前地父类的方法  
I am Tim,I am a speaker!My topic is Python
 

全部回复

0/140

量化课程

    移动端课程