码迷,mamicode.com
首页 > 编程语言 > 详细

2018-07-05-Python全栈开发day25-python中的继承

时间:2018-07-06 01:20:48      阅读:221      评论:0      收藏:0      [点我收藏+]

标签: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 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: Cant instantiate abstract class son with abstract methods read#必须再写一个read方法来和基类一致

 

2018-07-05-Python全栈开发day25-python中的继承

标签:ace   call   test   self   mos   row   print   imp   color   

原文地址:https://www.cnblogs.com/hai125698/p/9271311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!