本文参考子 <零基础学python>
1. 什么是类 对象 属性。 : 举例说明,汽车模型是类 ,每台汽车是对象,汽车的车牌标识每一台汽车 是属性。汽车的车牌、颜色、价格等都属于属性。类是对对象的抽象。
python 类里面: self作为前缀的称为实例属性。 静态变量称为类变量,类变量为所有实例共享。
class Fruit:
price=0 类属性
def __init()__:
self.color ='red' 实例属性
zone='china' 局部变量
self.__shape='round' 私有属性 访问:instance._classname._attribute
def getPrice(self) 类的普通方法
print(Friut.price)
@ staticmethod 类的静态方法
def getPrice()
print(Friut.price)
def __getPrice() 类的私有函数
print(Friut.price)
@classmethod
def __getPrice(cls) 类方法 把类作为函数的第一个参数
print(cls.price)
count=staticmethod(___getPrice)
count=classmethod(___getPrice)
if __main__()
apple=Fruit()
print apple.color().
上面代码的主程序可以直接访问Fruit的属性color ,这种直接暴露数据的方法是不提倡的。因为这种开发方式可以随意的更改实例属性,会导致数据安全方面的问题。通常做法是定义相关的get 方法获取实例属性。
如果某个方法需要被其他实例共享,同时又需要使用当前实例的属性,则定义为类方法。
2.内部类 不提倡使用。
3. python 方法的动态性。 就是把一个已经定义的函数添加到一个类里面去。
class A
pass
def add(self): 定义在类A的外面
print 'grow....'
if __main__()
A.grow=add
a=A()
a.grow()
4. 多态
python的继承机制,使的子类可以具有父类的共有属性和方法,又可以自定义自己的属性和方法,扩充自身功能,这种特性成为多态。
继承
例子1 : 子类的构造函数必须显示的调用父类的构造函数,来构造自己。
class Fruit:
def __init__(self,color):
self.color=color
print 'fruit's color is %s'%self.color
def grow(self):
print 'grow ........'
class Apple(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)
print 'apple color is %s'%self.color
class Banana(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)
print 'Bananacolor is %s'%self.color
def grow(self): 重写子类和父类的同名方法 ,子类会覆盖父类的方法
print 'banana grow ........'
调用:
if __main__()
a=Apple('red') 这里 参数 'red' 是传递给构造函数。
a.grow()
b=Banana()
b.grow()
上述 调用父类构造函数的方法 也可以用super类的super()方法 来调用父类__init__()方法。更加直观
super(type,object) type表示类 object 表示实例
例子2:
class A(object): # 在python2 里面必须要有object
def __init__(self):
print 'A init'
def grow(self):
print 'grow....'
class B(A):
def __init__(self):
super(B,self).__init__() #使用super直接调用更加直观 self是实例 使用super 调用父类的构造函数
print 'B init'
b=B()
b.grow()
抽象基类:
对于不同水果,生长不一样 因此此方法在父类里面应该是抽象的。
from abc import ABCMeta,abstractmethod
class Fruit(metaclass=ABCMeta):
def grow(self):pass
class Apple(Fruit):
def grow(self):
print('apple growing')
if __main__():
apple=Apple()
apple.grow()
多态性:在FruitShop 类中定义sellFruit()方法 ,这段代码sellFruit根据多态性的特性。
class Fruit:
def __init__(self,color):
pass
class Apple(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)
class Banana(Fruit):
def __init__(self,color):
Fruit.__init__(self,color)
class FruitShop:
def sellFruit(self,fruit): # 这里fruit 接收类的实例
if isinstance(fruit,apple):
print ''sell apple'
if isinstance(fruit,banana):
print ''sell banana'
if __main__ ():
shop=FruitShop()
apple=Apple('red')
banana=Banana('yellow')
shop.sellFruit(apple)
shop.sellFruit(banana)
多重继承 :比如 水果类 蔬菜类 -》 西红柿类 。
MIX机制:
假设 水果分为 削皮 剥皮 同时提供削皮方法和剥皮方法 ,那么继承方式如下
如果再分为 冬季和夏季水果那么 还要再分一层,这样复杂 且不容易维护 。那么可以采用MIX机制。
class Fruit(object): # 父类
def __init__(self):
pass
class HuskedFruit(object): # 削皮水果
def __init__(self):
print("initialize HuskedFruit")
def husk(self): # 削皮方法
print("husk ...")
class DecorticatedFruit(object): # 剥皮水果
def __init__(self):
print("initialize DecorticatedFruit")
def decorticat(self): # 剥皮方法
print("decorticat ...")
class Apple(HuskedFruit,Fruit):
pass
class Banana(DecorticatedFruit,Fruit):
pass
这样 不管你再怎么添加分类 都是在同一层次增加。