p1多继承
创建父类:
父亲类
class Father(object):
def _init_(self, money, play)
self.money = money
def play(self):
print('play')
def func(func1):
母亲类
class Mother(object):
def _init_(self, facevalue):
self.facevalue = facevalue
def eat(self):
print('eat')
def func(self):
print('func2')
创建子类:
孩子类
from Father import Father
from Mother import Mother
class Child(Father,Mother):
def _init_(self,money,faceValue):
Father.__init__(self, money)
Mother.__init__(self,faceValue)
frome Child import Child
def main():
c = Child(300, 100)
print(c.money, c.faceValue)
c.play()
c.eat()
# 父类中方法相同,默认调用的是在括号中派前面的父类的方法
c.func()
if __name__ == "__main__"
main()
p2多态
class Animal(object):
def __init__(self,name):
self.name = name
def eat(self):
print(self.name + '吃')
from animal import Animal
class Cat(Animal):
def __init__(self,name):
super(Cat, self)__init__(name)
from animal import Animal
class Mouse(Animal):
def __init__(self,name):
super(Mouse, self)__init__(name)
from cat import Cat
from mouse import Mouse
from person import Person
'''
多态:一种事务的多种形态
最终目标:人可以喂任何一种动物
'''
tom = Cat('tom')
jerry = Mouse('jerry')
tom.eat()
jerry.eat()
P3对象属性与类属性
对象属性的优先级高于类属性
对象属性只针对当前对象生效,对于类创建的其他对象没有作用
不要将类属性与对象属性重名,因为对象属性会屏蔽对象属性
文章评论