1-2-Python基础-OOP3-多继承单继承
Python基础 OOP-03
In [7]:
# 多继承的例子 # 子类可以直接拥有父类的属性和方法,私有属性和方法除外 class Fish(): def __init__(self,name): self.name = name def swim(self): print("i am swimming......") class Bird(): def __init__(self, name): self.name = name def fly(self): print("I am flying.....") class Person(): def __init__(self, name): self.name = name def work(self): print("Working........") # 单继承的例子 class Student(Person): def __init__(self, name): self.name = name stu = Student("yueyue") stu.work() # 多继承的例子 class SuperMan(Person, Bird, Fish): def __init__(self, name): self.name = name class SwimMan(Person, Fish): def __init__(self, name): self.name = name s = SuperMan("yueyue") s.fly() s.swim()
In [8]:
# 菱形继承问题 class A(): pass class B(A): pass class C(A): pass class D(B,C): pass
In [10]:
# 构造函数例子 class Person(): # 对Person类进行实例化的时候 # 姓名要确定 # 年龄得确定 # 地址肯定有 def __init__(self): self.name = "NoNmae" self.age = 18 self.address = "Studentwhonheim" print("In init func") # 实例化一个人 p = Person()
In [13]:
# 构造函数的调用顺序 - 1 # 如果子类没有写构造函数,则自动向上查找,知道找到位置 class A(): def __init__(self): print("A") class B(A): def __init__(self): print("B") class C(B): pass # 此时,首先查找C的构造函数 # 如果没有,则向上按照MRO顺序查找父类的构造函数,知道找到为止 c = C()
In [14]:
# # 构造函数的调用顺序 - 2 class A(): def __init__(self): print("A") class B(A): def __init__(self, name): print("B") print(name) class C(B): pass # 此时,首先查找C的构造函数 # 如果没有,则向上按照MRO顺序查找父类的构造函数,知道找到为止 # 此时,会出现参数结构不对应错误 c = C()
In [17]:
# # 构造函数的调用顺序 - 3 class A(): def __init__(self): print("A") class B(A): def __init__(self, name): print("B") print(name) class C(B): # c中想扩展B的构造函数, # 即调用B的构造函数后在添加一些功能 # 由两种方法实现 ''' # 第一种是通过父类名调用 def __init__(self, name): # 首先调用父类构造函数 B.__init__(self, name) # 其次,再增加自己的功能 print("这是C中附加的功能") ''' # 第二种,使用super调用 def __init__(self, name): # 首先调用父类构造函数 super(C, self).__init__(name) # 其次,再增加自己的功能 print("这是C中附加的功能") # 此时,首先查找C的构造函数 # 如果没有,则向上按照MRO顺序查找父类的构造函数,知道找到为止 # 此时,会出现参数结构不对应错误 c = C("我是C")
In [22]:
# Mixin案例 class Person(): name = "liuying" age = 18 def eat(self): print("EAT.......") def drink(self): print("DRINK......") def sleep(self): print("SLEEP.....") class Teacher(Person): def work(self): print("Work") class Student(Person): def study(self): print("Study") class Tutor(Teacher, Student): pass t = Tutor() print(Tutor.__mro__) print(t.__dict__) print(Tutor.__dict__) print("*"*20) class TeacherMixin(): def work(self): print("Work") class StudentMixin(): def study(self): print("Study") class TutorM(Person, TeacherMixin, StudentMixin): pass tt = TutorM() print(TutorM.__mro__) print(tt.__dict__) print(TutorM.__dict__)
In [28]:
# issubclass class A(): pass class B(A): pass class C(): pass print( issubclass(B, A)) print( issubclass(C, A)) print( issubclass(B, object))
In [31]:
# isinstance class A(): pass a = A() print(isinstance(a, A)) print(isinstance(A, A))
In [34]:
# hasattr class A(): name = "NoName" a = A() print(hasattr(a, "name" )) print(hasattr(a, "age" ))
In [36]:
# help案例 # 我想知道setattr的具体用法 help(setattr)
In [39]:
# dir 案例 class A(): pass #dir(A) a = A() dir(a)
Out[39]: