2018-07-05-Python全栈开发day25-python中的继承
Posted 叶海宾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-07-05-Python全栈开发day25-python中的继承相关的知识,希望对你有一定的参考价值。
# class Eat: # def __init__(self): # print(‘eat‘) # def eat(self): # print(‘eat‘) # # class Run: # def __init__(self): # print(‘run‘) # class Cat(Eat,Run): # def __init__(self): # print(‘this is a cat‘) # def eat(self): # print(‘this is sons eat‘) # class Dog(Eat,Run): # def __init__(self): # print(‘this is a dog‘) # p1=Cat() # p1.eat()
# class Father: # def __init__(self,name,age,gender): # self.name=name # self.age=age # self.gender=gender # def test(self): # print(‘this is father s way‘) # class Son(Father): # def __init__(self,name,age,gender,school):#如果儿子和父亲在name,age,gender等方面都一样,笨方法是再写一遍 # # self.name = name # # self.age = age # # self.gender = gender#接着再写子类独有的属性 # # Father.__init__(self,name,age,gender)#此方法如果父类的名字改变,则没法子 # super().__init__(name,age,gender)#方便之处是不用在意父类的名字改变,而且不用加self # self.school=school # def test(self): # # Father.test(self)#最古老的方法 # super().test() # # p1=Son(‘yehiabin‘,‘18‘,‘male‘,‘sdau‘) # p1.test() # # print(p1.name)
import abc class jilei(metaclass=abc.ABCMeta): @abc.abstractmethod def write(self): print(‘this is write‘) @abc.abstractmethod def read(self): print(‘this is read‘) class son(jilei): def write(self): print(‘this is son‘) p1=son() p1.write()
Traceback (most recent call last): File "C:/Users/brown/PycharmProjects/python_s3/dya25/继承.py", line 64, in <module> p1=son() TypeError: Can‘t instantiate abstract class son with abstract methods read#必须再写一个read方法来和基类一致
以上是关于2018-07-05-Python全栈开发day25-python中的继承的主要内容,如果未能解决你的问题,请参考以下文章