1. Combination concept
Objects of one class as properties of objects of another class
First example:
class Weapon:
def prick(self, obj): # 这是该装备的主动技能,扎死对方
obj.life_value -= 500 # 假设攻击力是500
class Person: # 定义一个人类
role = 'person' # 人的角色属性都是人
def __init__(self, name):
self.name = name # 每一个角色都有自己的昵称;
self.weapon = Weapon() # 给角色绑定一个武器;
egg = Person('egon')
egg.weapon.prick()
#egg组合了一个武器的对象,可以直接egg.weapon来使用组合类中的所有方法
The second example calculates the area of the circle
from math import pi
class Circle:
def __init__(self,r):
self.r = r
def area(self):
return pi * self.r ** 2 #求园的面积
def perimeter(self):
return self.r *pi * 2 #求园的周长
class Ring:
def __init__(self,outside_r,inside_r):
self.out_circle = Circle(outside_r)
self.in_circle = Circle(inside_r)
def area(self):
return self.out_circle.area() - self.in_circle.area() #外环面积-内环面积
def perimeter(self):
return self.out_circle.perimeter() + self.in_circle.perimeter()
r = Ring(10,5) #外环的半径和内环的半径
print(r.area())
print(r.perimeter())
2. Inheritance
Inheritance is a way to create new classes. In python, a newly created class can inherit one or more parent classes, which can also be called base classes or super classes, and newly created classes are called derived classes or subclasses. Example
: dog fight
class Animal: #父类
def __init__(self,name,dps,hp):
self.name=name
self.dps=dps
self.hp=hp
def eat(self):
print('%s吃药,回血了'%(self.name))
class Person(Animal):#子类
def __init__(self,name,dps,hp,sex):
super().__init__(name,dps,hp) #第二中写法 Animal.__init__(self,name,dps,hp)
self.sex=sex
def attack(self,dog):
dog.hp -= self.dps
print('%s打了%s,%s掉了%s点血,%s还剩%s点血'%(self.name,dog.name,dog.name,self.dps,dog.name,dog.hp))
class Dog(Animal):#子类
def __init__(self,name,dps,hp,kind):
super().__init__(name,dps,hp)
self.kind=kind
def bite(self,person):
person.hp -= self.dps
print('%s咬了%s,%s掉了%s点血,%s还剩%s点血' % (self.name, person.name, person.name, self.dps, person.name, person.hp))
alex=Person('alex',5,250,'男')
ha2=Dog('小黑',200,2000,'藏獒')
print(alex.__dict__)
print(ha2.__dict__)
alex.attack(ha2)
ha2.bite(alex)
ha2.eat()
Example 2
class Foo:
def __init__(self):
self.func()
def func(self):
print('in Foo')
class Son(Foo):
def func(self):
print('in Son')
Son()
#打印结果是in son
Instantiate an object, find the self in its own method, and then execute the self in the parent class, that is, the self in the son, and then call func, so print in son
Three, multiple inheritance
Two types of python
- The classic class py3 is extinct and still exists in python2. In py2, as long as the programmer does not take the initiative to inherit object, this class is a classic class—— depth first
- new-style class python3 all classes are
diamond inheritance
class A:
def f(self):
print('in A')
class B(A):
def f(self):
print('in B')
super().f()
class C(A):
pass
def f(self):
print('in C')
super().f()
class D(B,C):
def f(self):
print('in D')
super().f()
d = D()
d.f()
print(D.mro()) #可以查看继承的顺序
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
#打印结果
in D
in B
in C
in A
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
Super and finding the parent class are two different things
- In single inheritance, super is to find the parent class
- In the multi-level inheritance, the trajectory of super is expanded according to the starting point of the entire model in a breadth-first order that follows the mro rule
Example 2
class A:
def f(self):
print('in A')
class B(A):
pass
# def f(self):
# print('in B')
class C(A):
pass
# def f(self):
# print('in C')
class D(B,C):
pass
# def f(self):
# print('in D')
class E(C):
pass
# def f(self):
# print('in B')
class F(D,E):
pass
# def f(self):
# print('in C')
# d = D()
# d.f()
print(F.mro())