zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    systemmap 使用记录
    reading code record
    吞吐问题
    debug cps 原因
    fopen的a+和rewind
    debug cps && perf debug
    tfo以及quic的阅读笔记
    ss 和netstat
    debug open files
    多核编程 local global
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9537571.html
Copyright © 2011-2022 走看看