zoukankan      html  css  js  c++  java
  • 面向对象总结

    在python3中,无论是否继承object,都默认继承object,即python3中所有类均为新式类,而且都是菱形结构
    
    只有在python2中才分新式类和经典类,python3中统一都是新式类

    子类中调用父类的方法

    方法一:指名道姓,即父类名.父类方法()   强调:二者使用哪一种都可以,但最好不要混合使用 

    #_*_coding:utf-8_*_
    __author__ = 'Linhaifeng'
    
    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.line=line
    
        def run(self):
            print('地铁%s号线欢迎您' %self.line)
            Vehicle.run(self)
    
    line13=Subway('中国地铁','180m/s','1000人/箱','',13)
    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):
            #super(Subway,self) 就相当于实例本身 在python3中super()等同于super(Subway,self)
            super().__init__(name,speed,load,power)
            self.line=line
    
        def run(self):
            print('地铁%s号线欢迎您' %self.line)
            super(Subway,self).run()
    
    class Mobike(Vehicle):#摩拜单车
        pass
    
    line13=Subway('中国地铁','180m/s','1000人/箱','',13)
    line13.run()
  • 相关阅读:
    java连接zookeeper实现zookeeper的基本操作
    spring boot 中用@value给static变量赋值
    jquery 点击移动一次body
    Javascript实现导航锚点滚动效果实例
    vue.js 脚手架vue-cli构建了项目,想去除Eslint验证,如何设置?
    jquery 点击切换值
    js同时(onclick)调用多个方法
    修改输入框placeholder文字默认颜色css用法
    js获取星期几
    js计算器
  • 原文地址:https://www.cnblogs.com/dingyutao/p/9147455.html
Copyright © 2011-2022 走看看