四种python 单继承的实现方式
第一类单继承的实现:
from person import Person
from student import Student
from worker import Worker
per = Person("aa", 1, 2)
stu = Student("tom", 18, 12345, 110)
print(stu.name, stu.age)
stu.run()
print(stu.stuId)
#print(stu.__money)私有属性
print(stu.getMoney()) #通过继承过来的共有方法访问私有属性
#stu.stuFunc()
wor = Worker("lilei", 20, 111)
print(wor.name, wor.age)
wor.eat("apple")
print(per.getMoney()
第二类person:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Person(object):
def __init__(self, name, age, money):
self.name = name
self.age = age
self.__money = money
def setMoney(self, money):
self.__money = money
def getMoney(self):
return self.__money
def run(self):
print("run")
def eat(self, food):
print("eat " + food)
第三类student:
from person import Person
class Student(Person):
def __init__(self, name, age, money, stuId):
#调用父类中的__init__
super(Student, self).__init__(name, age, money)
#子类可以有一些自己独有的属性
self.stuId = stuId
def stuFunc(self):
print(self.__money)
第四类worker:
from person import Person
class Worker(Person):
def __init__(self, name, age, money):
#调用父类中的__init__
super(Worker, self).__init__(name, age, money)