标签:bar 执行 pre self catch 重写 私有 dong 除了
# 子类自动拥有父类中除了私有内容外的其他所有内容
# 王思聪(熊猫TV) -> 王健林(年龄)
# 继承目的: 对父类进行扩展
class Foo:
def getMoney(self):
print("拿钱")
#
class Bar(Foo):
pass
#
#
b = Bar()
b.getMoney() # 此时调用的是父类中的方法
# 当出现xxx是一种yyy类型的东西. 可以使用继承关系
# 猫是一种动物
class Animal:
def dong(self):
print("动物会动, 顾雍")
#
class Cat(Animal): # 子类其实是对父类的一种扩展
def catchMouse(self):
print("猫很皮, 抓老鼠")
c = Cat()
c.dong()
c.catchMouse()
a = Animal() # 父类的对象不能执行子类中的功能
a.dong()
a.catchMouse() # 创建的是动物. 动物不能执行抓老鼠
class Animal:
def dong(self):
print("动物会动, 顾雍")
#
class Cat(Animal): # 子类其实是对父类的一种扩展
def dong(self): # 子类中写了和父类一模一样的方法. 这个叫方法的覆盖, 重写
print("猫上蹿下跳")
def catchMouse(self):
print("猫很皮, 抓老鼠")
c = Cat() # 创建的是猫.
c.dong() # 类中的方法的查询顺序. 先找自己, 然后再找父类
# python支持多继承
class Foo1:
def getMoney(self):
print(‘给你个大嘴巴子‘)
def play(self):
print("玩儿")
class Foo2:
def getMoney(self):
print(‘给多点儿‘)
class Bar(Foo1, Foo2): # 离当前类最近的是亲爹, 后面的是干爹.
pass
b = Bar() # 就近原则, MRO的C3算法
b.getMoney() # Foo2里的
标签:bar 执行 pre self catch 重写 私有 dong 除了
原文地址:https://www.cnblogs.com/demons97/p/10134141.html