zoukankan      html  css  js  c++  java
  • 继承后构造函数的关系

    子类继承自父类之后,如果子类没有写构造函数,默认用父类的构造函数,传入父类的参数

    class Animal:
        def __init__(self, name, color):
            self.name=name
            self.color=color
     
        def eat(self):
            print('')
        def drink(self):
            print('')
        def run(self):
            print('')
        def sleep(self):
            print('')
    
    class Dog(Animal):
    
        def bark(self):
            print('%s 在叫'% (self.name))
    
    dog=Dog("小花","grey")
    dog.bark()

    如果子类自己有构造函数,默认用子类构造函数

    class Animal:
        def __init__(self, name, color):
            self.name=name
            self.color=color
     
        def eat(self):
            print('')
        def drink(self):
            print('')
        def run(self):
            print('')
        def sleep(self):
            print('')
    
    class Dog(Animal):
        def __init__(self, name):
            self.name=name
    
        def bark(self):
            print('%s 在叫'% (self.name))
    
    dog=Dog("小花")
    dog.bark()
  • 相关阅读:
    div3--C. Pipes
    Problem F Free Weights
    H
    Problem C Careful Ascent
    Problem L. World Cup
    Problem E. Bet
    Problem D. Ice Cream Tower
    A. Number Theory Problem
    A
    软考知识点梳理--项目评估
  • 原文地址:https://www.cnblogs.com/jestin/p/11972987.html
Copyright © 2011-2022 走看看