• Python 类的继承


    #继承
    
    class Person:
            def eat(self):
                    print("eating ...")
            def run(self):
                    print("runing ...")
    
    #继承的语法
    class Student(Person):
            def study(self):
                    print("study ...")
    
    
    stu1 = Student()
    
    stu1.run()
    #重写
    
    class Person:
            def eat(self):
                    print("eating ...")
            def run(self):
                    print("runing ...")
    
    #子类重写父类方法
    class Student(Person):
            def run(self):
                    print("quick runing ...")
                    print("father func ")
                    #子类中调用父类方法
                    #第一种方法:注意此时需要传参self
                    Person.run(self)
    
                    #第二种方法:通过super()方法调用父类
                    super().run()
    
            def study(self):
                    print("study ...")
    
    
    stu1 = Student()
    
    stu1.run()
    #类中私有方法或者私有属性的继承
    
    
    class Person:
            def __init__(self):
                    self.name = "tom"
                    self.__age = 14
    
            def __getTom(self):
                    print(self.__age)
    
    
    
    class Student(Person):
            def show(self):
                    #子类无法继承父类的私有成员属性
                    #print("name is %s and age is %d ."%(self.name,self.__age))
                    print("name is %s"%(self.name))
            def showtom(self):
                    #子类无法继承父类的私有方法
                    #__getTom()
    
    
    
    stu = Student()
    
    stu.showtom()
  • 相关阅读:
    写在毕业季前
    使用Github Page鼓励自己每日编程
    win8/Metro开发系列一 Xaml布局
    AlertDialog详解
    安卓项目文件目录
    Andriod布局之LinearLayout
    Andriod定时任务
    android 设置布局横屏竖屏
    Android默认启动程序问题
    Android全屏显示
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9537571.html
Copyright © 2020-2023  润新知