面向对象进阶
面向对象进阶
类的实例化过程
class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
def print_score(self):
print('%s:%s'%(self.name,self.score))
stu = Student('zhangsan','59')
stu.print_score()
________________________________________________
>>>:'zhangsan:59'
据上图我们得知,其实self,就是实例本身!实例化时python会自动把这个实例本身通过self参数传进去。
类的属性
-
公有属性
在类里直接定义的属性为公有属性
修改公有属性:
类名.属性=xxxxx #修改全局
实例化成员.属性=xxx #相当于创建一个变量,与上述属性并没有关联 -
私有属性
外部不可见,只能内部访问
self.__heart
若需要访问私有属性,需要对外提供一个只读访问接口
def get_heart(self):
return self.__heart
强制访问某个私有属性
r1._Obj__heart
- 成员属性
在构造函数 __init__中定义
class Person(object):
number = 0 #公有属性
def __init__(self,name):
self.name = name #成员属性
self.__heart = 'heart' #私有属性
def get_heart(self): #私有属性只读访问接口
return (self.__heart)
s1 = Person('zhangsan')
print(s1.name,s1.number)
s1.number = 2 #给实例绑定一个number属性
Person.number = 3
print(s1.number,Person.number)
del s1.number #删除实例的number属性
print(s1.number)
print(s1.__heart)
d= s1.get_heart()
print(d)
d =s1_Person__heart
print(d)
---------------------------------------------------
>>>:zhangsan 0
>>>:2,3
>>>:3
>>>:'Person' object has no attribute '__heart'
>>>:'heart'
>>>:'heart'
---------------------------------------------------
从上面的例子可以看出,在编写程序的时候,千万不要把实例属性和类属性使用相同的名字,因为相同名称的实例属性将屏蔽掉类属性,但是当你删除实例属性后,再使用相同的名称,访问到的将是类属性。
###**新式类,经典类继承**
- 新式类:
class School(object): #object是最基础类
def __init__(self,name):
self.name = name
class Student(School):
def __init__(self,name)
super(Student,self).__init(name) #新式类继承
- 经典类:
class School:
def __init__(self,name):
self.name = name
class Student(School):
def __init__(self,name):
School.__init__(self,name)
- 继承顺序
- Python2 当类是经典类时,多继承情况下,会按照深度优先方式查找(上→下)
- Python2 当类是新式类时,多继承情况下,会按照广度优先方式查找(左→右)
- Python3 版本中的类继承默认就是广度优先
#python 2
#新式继承
class A(object):
def __init__(self):
print 'from A'
class B(A):
def __init__(self):
print 'this is B channel'
super(B,self).__init__()
print 'from B'
class C(A):
def __init__(self):
print 'this is C channel'
super(C,self).__init__()
print 'from C'
class D(B,C):
def __init__(self):
print 'this is D channel'
super(D,self).__init__()
print 'from D'
-----------------------------------------
b=B()
>>>:this is B channel
>>>:from A
>>>>:from B
-----------------------------------------
c=C()
>>>:this is C channel
>>>:from A
>>>:from C
-----------------------------------------
d=D()
>>>:this is D channel
>>>:this is B channel
>>>:this is C channel
>>>:from A
>>>:from C
>>>:from B
>>>:from D
#python 2
#经典类继承
class A:
def __init__(self):
print 'from A'
class B(A):
def __init__(self):
print 'this is B channel'
A.__init__(self)
print 'from B'
class C(A):
def __init__(self):
print 'this is C channel'
A.__init__(self)
print 'from C'
class D(B,C):
def __init__(self):
print 'this is D channel'
B.__init__(self)
C.__init__(self)
print 'from D'
-----------------------------------------
b=B()
>>>:this is B channel
>>>:from A
>>>:from B
-----------------------------------------
c=C()
>>>:this is C channel
>>>:from A
>>>:from C
-----------------------------------------
d=D()
>>>:this is D channel
>>>:this is B channel
>>>:from A
>>>:from B
>>>:from D
>>>:this is C channel
>>>:from A
>>>:from C
>>>:from D