1 # 2 # @author:浊浪 3 # @version:0.1 4 # @time: 2021/4/17 9:34 5 # 6 7 8 class A: 9 pass 10 class B: 11 pass 12 class C(A,B): 13 def __init__(self,name,age): 14 self.name = name 15 self.age = age 16 class D(A): 17 pass 18 19 # 创建C类的对象 20 shili = C('张三', 18) 21 print(shili.__dict__) # {'name': '张三', 'age': 18} 输出实例对象的字典 22 print(C.__dict__) # {'__module__': '__main__', '__init__': <function C.__init__ at 0x01A7A8E0>, '__doc__': None} 23 print('________________________') 24 print(shili.__class__) # <class '__main__.C'> 输出实例对象的所属类 25 print(C.__bases__) # (<class '__main__.A'>, <class '__main__.B'>) 输出C的父类 26 print(C.__base__) # <class '__main__.A'> 输出C的一个父类 27 print(C.__mro__) # 输出C类的层次结构 (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
1 # 2 # @author:浊浪 3 # @version:0.1 4 # @time: 2021/4/17 9:54 5 # 6 7 class Student: 8 def __init__(self, name): 9 self.name = name 10 def __add__(self, other): 11 return self.name + other.name 12 def __len__(self): 13 return len(self.name) 14 15 stu1 = Student('张三') 16 stu2 = Student('李四') 17 18 s = stu1 + stu2 #实现了两个对象的加法运算(因为在Student类中编写_add()特殊的方法) 19 print(s) # 张三李四 20 x = stu2.__add__(stu1) 21 print(x) # 李四张三 22 23 print('----------------------') 24 25 lst = [11, 22, 33, 44, 55] 26 print(len(lst)) #5 27 print(lst.__len__()) #5 28 print(stu1.__len__()) #2