python新类似乎违背了广度优先的执行顺序, 对象自定义计数实例化的多少

class TSSS():
    def f1(self):
        print('from TSSS')


class SSS(TSSS):
    def f1(self):
        print('from SSS')

class SS():
    def f1(self):
        print('from SS')

class S(SSS):
    # def f1(self):
    #     print('from S')
    pass

class A(S):
    def f1(self):
        print('from A')
        super().f1()

class B(SS):
# class B:
    def f1(self):
        print('from B')

class C(A,B):
    def f1(self):
        print('from C')
        super().f1()
    pass
print(C.mro())
c = C()
c.f1()

print(C.mro())

输出:

[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.S'>, <class '__main__.SSS'>, <class '__main__.TSSS'>, <class '__main__.B'>, <class '__main__.SS'>, <class 'object'>]
from C
from A
from SSS
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.S'>, <class '__main__.SSS'>, <class '__main__.TSSS'>, <class '__main__.B'>, <class '__main__.SS'>, <class 'object'>]

第二种情景

class TSSS():
    def f1(self):
        print('from TSSS')


class SSS(TSSS):
    def f1(self):
        print('from SSS')

class SS(TSSS):
    def f1(self):
        print('from SS')

class S(SSS):
    # def f1(self):
    #     print('from S')
    pass

class A(S):
    def f1(self):
        print('from A')
        super().f1()

class B(SS):
# class B:
    def f1(self):
        print('from B')

class C(A,B):
    def f1(self):
        print('from C')
        super().f1()
    pass
print(C.mro())
c = C()
c.f1()

print(C.mro())

输出:

[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.S'>, <class '__main__.SSS'>, <class '__main__.B'>, <class '__main__.SS'>, <class '__main__.TSSS'>, <class 'object'>]
from C
from A
from SSS
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.S'>, <class '__main__.SSS'>, <class '__main__.B'>, <class '__main__.SS'>, <class '__main__.TSSS'>, <class 'object'>]

对象自定义计数实例化的多少

* encoding: utf-8 * @author: ty

class LuffyStudent:
    school = 'Luffycity'
    count =0

    def __init__(self,name,sex,age):
        self.name1 = name
        self.sex1 = sex
        self.age1 = age
        self.count += 1         #私有的属性
        LuffyStudent.count +=1    ##公有的属性


    def learn(self):
        print('%s is learning'%self.name1)

    def eat(self):
        print('%s is eating' % self.name1)

stu1 = LuffyStudent('王二丫','女',18)
stu2 = LuffyStudent('李三炮','男',19)
stu3 = LuffyStudent('张铁蛋','男',18)
LuffyStudent.count
print(stu1.count)
print(stu2.count)
print(stu3.count)
print(stu1.__dict__)
print(stu2.__dict__)
print(stu3.__dict__)

输出:

1
2
3
{'name1': '王二丫', 'sex1': '女', 'age1': 18, 'count': 1}
{'name1': '李三炮', 'sex1': '男', 'age1': 19, 'count': 2}
{'name1': '张铁蛋', 'sex1': '男', 'age1': 18, 'count': 3}
posted @ 2022-10-16 20:52  ty1539  阅读(13)  评论(0编辑  收藏  举报