Day20作业及默写

时间:2022-09-24 20:01:54

1.请使用C3算法计算出链接图中的继承顺序-Link

graph BT
    id1[A]-->id2[B]
    id2[B]-->id6[F]
    id6[F]-->id7[G]
    id1[A]-->id3[C]
    id3[C]-->id5[E]
    id1[A]-->id4[D]
    id4[D]-->id7[G]
    id7[G]-->id0[object]
G = [GO]
F = [FGO]
E = [EO]
B = B + [FGO] = [BFGO]
C = C + [EO] = [CEO]
D = D + [GO] = [DGO]
A = A + [BFGO] + [CEO] + [DGO]
A = [BFGO] + [CEO] + [DGO]
AB = [FGO] + [CEO] + [DGO]
ABF = [GO] + [CEO] + [DGO]
ABFC = [GO] +[EO] + [DGO]
ABFCE = [GO] +[O] + [DGO]
ABFCED = [GO] +[O] + [GO]
ABFCEDG = [O] +[O] + [O]
ABFCEDGO
A -- > B -- > F -- > C -- > E --> D --> G --> object

graph BT
    id1[A]-->id2[B]
    id2[B]-->id4[D]
    id2[B]-->id5[E]
    id4[D]-->id6[F]
    id6[F]-->id0[object]
    id1[A]-->id3[C]
    id3[C]-->id5[E]
    id5[E]-->id7[G]
    id7[G]-->id0[object]
F = [FO]
G = [GO]
D = D + [FO] = [DFO]
E = E + [GO] = [EGO]
B = B + [DFO] + [EGO]  = [BDFEGO]
C = C + [EGO] = [CEGO]
A = A +  [BDFEGO] + [CEGO]
A =[BDFEGO] + [CEGO]
AB =[DFEGO] + [CEGO]
ABD =[FEGO] + [CEGO]
ABDF =[EGO] + [CEGO]
ABDFC =[EGO] + [EGO]
ABDFCEGO
A -- > B -- > D -- > F -- > C -- > E -- > G -- > object

graph BT
    id1[A]-->id2[B]
    id2[B]-->id4[D]
    id2[B]-->id5[E]
    id4[D]-->id0[object]
    id5[E]-->id7[G]
    id7[G]-->id0[object]
    id1[A]-->id3[C]
    id3[C]-->id4[D]
    id3[C]-->id6[F]
    id6[F]-->id0[object]
G = [GO]
D = [DO]
E = E + [GO] = [EGO]
F = [FO]
B = B + [DO] + [EGO] = [BDEGO]
C = C + [DO] + [FO] = [CDFO]
A = A + [BDEGO] + [CDFO]
A = [BDEGO] + [CDFO]
AB = [DEGO] + [CDFO]
ABC = [DEGO] + [DFO]
ABCD = [EGO] + [FO]
ABCDEGFO
A -- > B -- > C -- > D -- > E --> G -- > F -- > object

2.请自己找一张类的继承图,按照C3算法说出它的继承顺序

3.运行代码,请说出结果,并说出为什么结果是这样?

class Foo:
      def __init__(self):
             self.func()
      def func(self):
             print('in foo')
class Son(Foo):
      def func(self):
             print('in son')
Son()
#Son() == Foo.__init__(self)
#Foo.__init__(self) == Foo.func()
#Foo.func() --> Son.func()
#print('in son')

4.继续完成计算器作业

5.默写 使用继承完成的人狗大战程序

class Animal(object):
    def __init__(self,name,blood,ad):
        self.name = name
        self.hp = blood
        self.ad = ad
class Dog(Animal):
    def __init__(self,name,blood,ad,kind):
        super().__init__(name,blood,ad)
        self.kind = kind

    def bite(self,person):
        person.hp -= self.ad
        print('%s攻击了%s,%s掉了%s点血' % (self.name, person.name, person.name, self.ad))

class Person(Animal):
    def __init__(self,name,hp,ad,sex):
        super().__init__(name,hp,ad)
        self.sex = sex

    def fight(self,dog):
        dog.hp -= self.ad
        print('%s攻击了%s,%s掉了%s点血'%(self.name,dog.name,dog.name,self.ad))

hei = Dog('小黑',300,20,'哈士奇')
alex = Person('alex',20,1,'不详')
alex.fight(hei)
print(hei.hp)
hei.bite(alex)
print(alex.hp)