# # 继承 is a :类之间的继承 has a # '''所有的类都叫自定义类型 # 公路(road): # 属性,公路名称,公路长度 # # 车(car): # 属性 车名 车速 # 方法1.求车名在那条公路上以多少时速行走了多远 # get_time(self,road) # 2.初始化车属性信息__init__方法 # 3.打印对象显示车的属性信息 # ''' # # # class Road: # def __init__(self, road_name, road_long): # self.road_name = road_name # self.road_long = road_long # # # class Car: # def __init__(self, car_name, car_sud): # self.car_name = car_name # self.car_sud = car_sud # # def get_time(self, road): # r = road 指针指向的同一个空间 # print('{}在{}上走了{}'.format(self.car_name,self.car_sud,road.road_name)) # # def __str__(self): # return '{}{}'.format(self.car_name, self.car_sud) # # r= Road('zzz',18000) # a = Car('zz', 18) # a.get_time(r) # print(a) # ------------------------------------------------------------------------------------ # student book computer class Student: def __init__(self, name, book, computer): self.name = name self.books = [] self.books.append(book) self.computer = computer def __str__(self): return '{}{}{}'.format(self.name, str(self.books), str(self.computer)) # 这边最好还是学标准化好一点,不然老是报错 class Book: def __init__(self, bname, author, number): self.banme = bname self.author = author self.number = number def borrow_book(self): pass def __str__(self): return self.banme + '-----' + self.author + '------' + self.number class Computer: def __init__(self, brand, type, color): self.brand = brand self.type = type self.color = color def online(self): print('正在上网............') def __str__(self): return self.brand + '----' + self.type + '------' + self.color diannao = Computer('mac', 'pro', 'red') print(diannao) shuben = Book('盗墓笔记', '南派三叔', '10') print(shuben) x = Student('zhangzhehui', shuben, diannao) print(x) """ has a 就是一个对象里面包含了另外一些对象的问题,一个类里面我可以去使用一些其他的对象 is a 如果A是B的,那么B就是A的父类=base,好比等边三角形和三角形的关系 ValueError继承于Exception ------------------------------------------------------------------------------- class Student: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) class Employee: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) class Doctor: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) s = Student() s.eat() d = Doctor() e = Employee() # 明显这三个都是有共同的属性的,如果一个一个写的话太麻烦了 ---------------------------------------------------------------------------------------- 那么就直接提取共性,给他们继承,方法如下: class Preson: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) class Student(Preson): pass class Employee(Preson): pass class Doctor(Preson): pass s = Student() 注意这样的这么是有灰色的,要填入参数 s = Student('zzh',18) s.eat() #zzh正在吃饭 d = Doctor() e = Employee() """ class Preson: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) class Student(Preson): pass class Employee(Preson): pass class Doctor(Preson): pass s = Student('zzh',18) s.eat()
Debug可以看出 其实还是在student类里面进行了空间的创建
只不过一个问题是student是从preson里面来的,所以底是在person里面的
所以只要找不到东西就会往上面找,从儿子找到爷爷
子类父类中都有init的情况
class Preson: def __init__(self, name, age): self.name = name self.age = age def eat(self): print('{}正在吃饭'.format(self.name)) class Student(Preson): def __init__(self): print('11121231456412156') #如何调用父类里面的init呢? super().__init__() #这表示找到父类里面的init 共存 def shop(self): print('zhengazimaidongxi') -------------------------------------------------------------------- 父类子类都有一个函数的时候 class Preson: def __init__(self, name, age): self.name = name self.age = age def study(self): print('正在学习做人') class Student(Preson): def __init__(self, name, age, clazz): super().__init__(name, age) self.clazz = clazz def study(self): print('{}正在学习课程'.format(self.name)) s = Student('zzh', 18, 'python') s.study()
1.如果类中不定义__init__,调用父类中的__init__ 2.如果当前的父类中也有需要调用的部分,就需要调用继承一下父类,方法如下: super().__init__(参数) super(类名名字,对象).__init__(要调用的参数) 3.如果父类子类都有eat,那么会优先子类的eat,所以有时候会有一个重写的过很override super().study() 这种就可以同时父类+子类,先父 ---------------------------------------------------------------------------# class Preson: # def __init__(self, name): # self.name = name # # def eat(self): # print('我再吃') # # def eat(self, food): # print('w z c {}'.format(food)) # # # p = Preson('zzz') # p.eat('5') # TypeError: eat() missing 1 required positional argument: 'food' # # 有同名的函数,因为他的顺序是 从上到下,就会直接覆盖掉 # 多继承: # class A: # def test1(self): # print('AAAAAAA') # # class B: # def test2(self): # print('BBBBBBBBBBBBB') # # class C(A,B): # def test3(self): # print('CCCC') # c = C() # c.test1() # c.test2() # c.test3() # 这就是多继承,只要继承了就会拥有相应父类中的东西 # 如果都是text 那么就会调用C # class A: # def test(self): # print('AAAAAAA') # # class B: # def test(self): # print('BBBBBBBBBBBBB') # # class C(A,B): # def test(self): # print('CCCC') # import inspect # # # class Basic(): # def text(self): # print('basic') # # class A(Basic): # def text (self): # print('AAAAA') # # class B(Basic): # def text(self): # print('BBBB') # # class C(Basic): # def text(self): # print('CCCC') # # class D(A,B,C): # pass # # d = D() d.text() #AAAAA print(inspect.getmro(D)) #(<class '__main__.D'>, <class '__main__.A'>, # <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Basic'>, <class 'object'>) # print(D.__mro__) 这是一个广度优先,从左到右