Python 类的继承
2021-07-15 01:04
标签:col .sh UNC 重写 ati init bsp 需要 super Python 类的继承 标签:col .sh UNC 重写 ati init bsp 需要 super 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9537571.html#继承
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()