面向对象高级语法部分
通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法。
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @staticmethod def talk(): print("I like to study python") class Teacher(SchoolMember): def __init__(self,name,age,sex,course,salary): super(Teacher,self).__init__(name,age,sex) self.course = course self.salary = salary def Teaching(self): print("Teacher %s is teaching %s." %(self.name,self.course)) s1 = Teacher("alex",22,"Femal","python",10000) print("before:",s1.member_nums) SchoolMember.member_nums = 12 print("before:",s1.member_nums) s1.member_nums = 666 #是在类中重新生成一个变量 print("after:",s1.member_nums) SchoolMember.member_nums = 12 print("after:",s1.member_nums)
在上面代码中,member_nums是类变量,如果直接调用s1.member_nums的话,调用的是类里面的值;如果s1.member_nums = 666,等于在实例里面增加了一个新的变量,这个时候,修改类的值的时候,是不会影响实例里面变量的值的。上面代码的输出结果如下:
before: 0
before: 12
after: 666
after: 666
类的静态方法@staticmethon:
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %SchoolMember.member_nums) @staticmethod def walk(self): print("%s is walking......" %self.name) #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.walk()
运行结果如下:
Traceback (most recent call last):
File "/home/zhuzhu/day7/staticmethon方法.py", line 22, in <module>
s1.walk()
TypeError: walk() missing 1 required positional argument: 'self'
上面代码中,如果没有@staticmethon的话,代码执行肯定没有问题,但是当有了@staticmethod的时候,系统提示少了一个参数。如果我们把一个方法变成了一个静态方法,那么这个方法与实例就没有太大的关系了。
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %SchoolMember.member_nums) @staticmethod #让方法在类中剥离,与类没有关系,调用要传递参数 def walk(self): print("%s is walking......" %self) #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.walk("alex")
@staticmethod静态方法是让类中的方法与类没有关联,调用的时候要传递参数才能调用。
类方法
类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) #@classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %self.name) SchoolMember.member_nums #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.talk()
上面代码中,(1)类是不能直接访问实例里面的属性的;(2)@classmethod的作用是让程序只能访问类中的变量,比如上面代码中的SchoolMember.member_nums,这个是类的方法,我们可以在talk中进行访问,但是不能访问self.name,因为@classmethod只能访问类属性。
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %self.name) SchoolMember.member_nums #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.talk() 运行结果如下: Traceback (most recent call last): File "/home/zhuzhu/day7/staticmethon方法.py", line 18, in <module> s1.talk() File "/home/zhuzhu/day7/staticmethon方法.py", line 13, in talk print("%s like to study python" %self.name) AttributeError: type object 'SchoolMember' has no attribute 'name'
从上面可以看出,上面的代码@classmethon禁止了类中的实例变量,只能使用类变量。即不能使用self.name、self.age和self.sex,只能使用self.nember_nums和SchoolMember.member_nums类的变量。如下:
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %SchoolMember.member_nums) SchoolMember.member_nums #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.talk() 运行结果如下: 0 like to study python
属性方法
属性方法的作用就是通过@property把一个方法变成一个静态属性
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %SchoolMember.member_nums) @property #把类的方法变成一个属性,调用要使用s1.walk不加括号直接调用,称为一个属性 def walk(self): print("%s is walking......" %self.name) #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.walk()
运行结果如下:
Traceback (most recent call last):
File "/home/zhuzhu/day7/staticmethon方法.py", line 22, in <module>
s1.walk()
TypeError: 'NoneType' object is not callable
如果不添加@property的话,程序是能够正常运行的,但是添加了@property之后,程序运行出现错误,是什么原因呢?因为@property是将类的方法变成了类的属性,调用的时候我们只需执行s1.walk()无需添加括号即可执行,如下:
class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex member_nums = 0 def introduce(self): print("My name is %s,and I am %s year old." %(self.name,self.age)) @classmethod #类方法,不能访问实例变量 def talk(self): print("%s like to study python" %SchoolMember.member_nums) @property #把类的方法变成一个属性,调用要使用s1.walk不加括号直接调用,称为一个属性 def walk(self): print("%s is walking......" %self.name) #SchoolMember.talk() #不能调用,类是没有办法访问实例变量,只能访问自己 s1 = SchoolMember("Alex",22,"Female") #实例化 s1.walk
运行结果如下:
Alex is walking......
上面代码中,@property是将类的方法变成了成员的属性,我们可以直接使用s1.walk即可调用。
经典类vs新式类
class A: #经典类的写法,新式类是A(object)尽量少用经典类,都用新式类现在 def __init__(self,name): self.name = name def f1(self): print("f1,搞基") class B(A): def __init__(self,name): super(B,self).__init__(name) # def f1(self): # print("f1,来呀") class C(A): def __init__(self,name): super(C,self).__init__(name) #def f1(self): #print("f1,一起搞!") class D(B,C): pass d = D("Alex") d.f1()
上面代码中,D类继承了B类和C类,当我们执行D类中的方法是,首先是在B类中进行查找,这个经典类和新式类都是一样的,如果查找不到,经典类是去A类中进行查找,而新式类是去C类中查找,实例如下:(备注:必须去2.X版本中运行才有差别,3.X进行优化,B类中查找不到都去C类中查找)
下面是经典类和新式类的执行顺序:
(新式类)先执行同级的类
(2)经典类(先执行上一级的类)