標籤:ace call test self mos row print imp color
# 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 abcclass 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中的繼承