zoukankan      html  css  js  c++  java
  • python面向对象的三大特征--继承子类调用父类方法

    #在子类中调用父类方法
    
    class Vehicle:
    
        country="China"
    
    
        def __init__(self,name,speed,load,power):
            self.name=name
            self.speed=speed
            self.load=load
            self.power=power
    
    
        def run(self):
            print("开动啦")
    
    
    class Subway(Vehicle):
    
        def __init__(self,name,speed,load,power,line):
            Vehicle.__init__(self,name,speed,load,power)
    
    
            # self.name = name
            # self.speed = speed
            # self.load = load
            # self.power = power
            self.line=line
    
    
        def show_info(self):
            print(self.name,self.line)
    
        def run(self):
            Vehicle.run(self)
            print("%s开动了"%self.name)
    
    
    line13=Subway("成都","1000m/s",1000000000,"",13)
    
    line13.show_info()
    
    
    line13.run()
    #使用super方法调用父类方法
    class
    Vehicle: country="China" def __init__(self,name,speed,load,power): self.name=name self.speed=speed self.load=load self.power=power def run(self): print("开动啦") class Subway(Vehicle): def __init__(self,name,speed,load,power,line): #Vehicle.__init__(self,name,speed,load,power) #super().__init__(name,speed,load,power) super(__class__,self).__init__(name,speed,load,power) super(Subway,self).__init__(name,speed,load,power) # self.name = name # self.speed = speed # self.load = load # self.power = power self.line=line def show_info(self): print(self.name,self.line) def run(self): # Vehicle.run(self) super().run() print("%s开动了"%self.name) line13=Subway("成都","1000m/s",1000000000,"",13) line13.show_info() line13.run() print(line13.__class__)
    如果我失败了,至少我尝试过,不会因为痛失机会而后悔
  • 相关阅读:
    Maven Docker镜像使用技巧
    Dockerfile 最佳实践
    无状态服务
    Docker 镜像加速器
    如何设置Docker容器中Java应用的内存限制
    k8s的容器监测探针
    (部署)使用kubernetes的deployment进行RollingUpdate
    linux下brctl配置网桥
    代码高亮插件
    Docker容器
  • 原文地址:https://www.cnblogs.com/tangcode/p/11301829.html
Copyright © 2011-2022 走看看