第一类单继承的实现:
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:
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)