一、封装
1.把属性封装到对象中,方便调用
2.把相同的功能封装同一方法中,方便调用
class Dog():
def __init__(self,name,age):
self.name = name
self.age = age
def jiao(self):
print('狗叫')
def sleep(self):
print('睡觉')
dog = Dog('哈哈',12)
dog.jiao()
二、继承
1.如果不同类中有相同的行为或属性,这个时候我们考虑到用继承,减少代码,实现代码复用
class Animal(object):
def __init__(self):
self.name = '傻子'
def eat(self):
print('吃饭')
class Dog(Animal):
pass
class Cat(Animal):
pass
dog = Dog()
dog.eat()
print(dog.name)
cat = Cat()
cat.eat()
2.继承之传递性
继承具有传递性,‘爷爷可以传给孙子’
class Animal(object):
def __init__(self):
self.name = '傻子'
def eat(self):
print('吃饭')
class Dog(Animal):
pass
class Hsq(Dog):
pass
hsq = Hsq()
hsq.eat()
3.继承之重写
当父类的行为不能满足字类的行为时,在所定义的类中写相应的行为来修改字类的行为
class Animal(object):
def __init__(self):
self.name = '傻子'
def eat(self):
print('吃饭')
class Dog(Animal):
def eat(self):
print('吃狗粮')
dog = Dog()
dog.eat()
4.继承之super
super().xxx 在合适的地方可以调用父类的方法
class Father(object): #父类 == 基类
def take_money(self):
print('经商赚钱')
class Son(Father): #字类 == 派生类
def take_money(self):
print('写代码赚钱')
super().take_money() #调用父类的方法
son = Son()
son.take_money()
5.私有属性和私有的方法继承不过来
class Father(object): #父类 == 基类
def __init__(self):
self.hit = '抽烟'
self.__info = '女朋友'
def take_money(self):
print('经商赚钱')
def __show(self):
print('哈哈哈')
class Son(Father): #字类 == 派生类
def take_money(self):
print('写代码赚钱')
super().take_money() #调用父类的方法
son = Son()
son.take_money()
print(son.hit)
# print(son.__info) 私有的属性继承不过来
#print(son.__show) 私有的方法行为继承不过来
6.继承之多继承
class A(object):
def a(self):
print('a方法')
class B(object):
def b(self):
print('b方法')
class C(object):
def c(self):
print('c方法')
class D(A,B,C):
pass
d = D()
d.a()
d.b()
d.c()
在py3当中,继承是广度继承,在py2中,类你不写object时,继承是深度继承,写上objec后是广度继承
class A(object):
def a(self):
print('a方法')
class B(A):
pass
class C(A):
def a(self):
print('a2方法')
class F(B,C):
pass
f = F()
f.a()
print(F.__mro__) #(<class '__main__.F'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
7.继承案例
#教学老师
class teacher(object): #父类
def __init__(self,name,age):
self.name = name
self.age = age
#规定了字类所拥有的功能,具体的还是看字类
def eat(self):
print('可以吃饭')
def play(self):
print('可以教人')
class basketball_teacher(teacher): #继承父类
def play(self): #继承重写
print('可以教人打篮球')
def eat(self): #继承重写
print('%s可以吃大米饭'%self.name)
class football_teacher(teacher):
def eat(self): #继承重写
print('%s可以吃炒饭'%self.name)
def sleep(self):
print('可以睡觉')
def play(self): #继承重写
print('可以教人踢足球')
laoshi = basketball_teacher('胡见',23)
laoshi.eat()
laoshi.play()
jiaolian = football_teacher('喻喻',21)
jiaolian.eat()
jiaolian.play()
#手机
class Call(object):
def __init__(self,xinghao,color):
self.xinghao = xinghao
self.color = color
def ddh(self,name):
print('{}的{}可以{}打电话'.format(self.color,self.xinghao,name))
def show(self):
if isinstance(self,Huawei): #isinstance()判断一个对象是否是属于某个类,语法是 isinstance(对象,类)
print('我的型号是%s,我的颜色是%s,我的大小是%s' % (self.xinghao, self.color, self.size))
else:
print('我的型号是%s,我的颜色是%s'%(self.xinghao,self.color))
class Huawei(Call):
def __init__(self,xinghao,color,size):
super().__init__(xinghao,color)
self.size = size
def movie(self):
print('%s可以看视频'%self.xinghao)
class Pingguo(Call):
def play_game(self):
print('{}可以玩游戏'.format(self.xinghao))
call = Huawei('华为','亮黑色',30)
pingguo = Pingguo('苹果','白色')
call.ddh('胡豆')
call.movie()
call.show()
pingguo.ddh('喻豌豆')
pingguo.play_game()
pingguo.show()
'''
树
苹果树
桃树
香樟树
'''
class Tree(object):
def __init__(self,name,color):
self.name = name
self.color = color
def kaihua(self):
print('{}可以开花'.format(self.name))
def show(self):
if isinstance(self,Apple):
print('我的名字是%s,我的颜色是%s,我的高度是%s米'%(self.name,self.color,self.height))
else:
print('我的名字是%s,我的颜色是%s' % (self.name, self.color))
def jieguo(self):
print('树可以结果子')
class Apple(Tree):
def __init__(self,name,color,height):
self.height = height
super().__init__(name,color)
class Taoshu(Apple):
pass
def jieguo(self):
print('%s可以结桃子'%self.name)
class Xiangzhangshu(Tree):
pass
apple = Apple('苹果树','绿色',23)
apple.show()
apple.jieguo()
taoshu = Taoshu('桃树','红色',41)
taoshu.show()
taoshu.kaihua()
taoshu.jieguo()
xiangzhangshu = Xiangzhangshu('香樟树',"蓝色")
xiangzhangshu.show()
三、多态
同一种事务,具有多种形态
前提需要有继承和重写
调用同一种行为,产生了不同的结果
class Ainaml():
def play(self):
print('玩')
class Dog(Ainaml):
def play(self):
print('在大马路上跑')
class Bird(Ainaml):
def play(self):
print('在天上飞')
class People(Ainaml):
def play_with_ainaml(self,A):
A.play()
dog = Dog()
bird = Bird()
people = People()
people.play_with_ainaml(bird)
people.play_with_ainaml(dog)