2018-07-05-Python全棧開發day25-python中的繼承

來源:互聯網
上載者:User

標籤: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中的繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.