标签:查找 继承 count 概念 base return python dict main
class ParentClass1:
pass
class ParentClass2:
pass
class SubClass(ParentClass1, ParentClass2):
pass
class ParentClass1:
def __init__(self, name, age):
self.name = name
self.age = age
class ParentClass2(ParentClass1):
def __init__(self, name, age, height):
super(ParentClass2, self).__init__(name, age)
self.height = height
class SubClass1(ParentClass2): # 普通的继承
pass
class SubClass2(ParentClass2): # 如果是多继承,查找顺序,按照mro算法
def __init__(self, gender, name, age, height):
# ParentClass1.__init__(self, name, age)
super().__init__(name, age, height)
self.gender = gender # 派生
sc = SubClass2('male', 'nick', 18, 180) # 实例化的时候自动调用__init__
class F1:
count =0
pass
class F2:
count = 1
pass
# f1 = F1()
# f2 = F2()
# f1.f2 = f2 # 组合
# print(f1.f2.count)
f1 = F1()
f2 = F2()
print(f2.__dict__)
f2.f1 = f1
print(f2.__dict__)
# f2.f1(key) = f1(value)
print(f2.__dict__['f1'].count)
print(f2.f1.count)
def f3():
return f1
f = f3() # f = f1
print(f.count)
def f4():
return F1
f = f4()
print(f().count)
class G(object):
# def test(self):
# print('from G')
pass
print(G.__bases__)
class E(G):
# def test(self):
# print('from E')
pass
class B(E):
# def test(self):
# print('from B')
pass
class F(G):
# def test(self):
# print('from F')
pass
class C(F):
# def test(self):
# print('from C')
pass
class D(G):
# def test(self):
# print('from D')
pass
class A(B, C, D):
def test(self):
print('from A')
obj = A()
for i in A.__mro__:
print(i)
'''
(<class 'object'>,)
<class '__main__.A'>
<class '__main__.B'>
<class '__main__.E'>
<class '__main__.C'>
<class '__main__.F'>
<class '__main__.D'>
<class '__main__.G'>
<class 'object'>
'''
# Python本身就是多态,根本就不支持多态
class Animal():
def eat(self):
print('eat')
class People(Animal):
def eat(self):
print('人吃')
class Dog(Animal):
def eat(self):
print('??吃')
class Pig(Animal):
def eat(self):
print('??吃')
def sleep(self):
print('??睡')
class F(Animal):
def eat(self):
print('f吃')
f = F()
peo = People()
pig = Pig()
dog = Dog()
# 多态性
peo.eat()
pig.eat()
dog.eat()
f.eat()
class Cat(Animal):
def eat(self):
print('??吃')
cat = Cat()
cat.eat()
# 100种动物
print('*'*50)
# 多态性的使用,提供接口的概念
def func(obj):
obj.eat()
obj.run()
obj.walk()
obj.sleep()
func(cat)
# cat.eat()
# cat.walk()
# cat.run()
# cat.sleep()
func(dog)
# dog.eat()
# dog.walk()
# dog.run()
# dog.sleep()
func(peo)
# 取钱,插卡-》输入密码-》输入金额-》取到钱了
# (插卡-》输入密码-》输入金额-》取到钱了)--》取钱函数
# 鸭子类型:只要长得像??,叫的像??,游泳也想??,你就是??
标签:查找 继承 count 概念 base return python dict main
原文地址:https://www.cnblogs.com/gaohuayan/p/11058916.html