1、继承
2、组合
3、继承和组合
在jichengandzuhe.py中
class People: def __init__(self, name, age, sex, year, mon, day): self.name = name self.age = age self.sex = sex self.birth = Date(year, mon, day) def walking(self): print(‘%s is walking ‘ % self.name) def talking(self): print(‘%s is talking ‘ % self.name) class Date: def __init__(self, year, mon, day): self.year = year self.mon = mon self.day = day def tell_birth(self): print(‘出生于<%s>年 <%s>月 <%s>日‘ % (self.year, self.mon, self.day)) class Teacher(People): def __init__(self, name, age, sex, level, salary, year, mon, day): # People.__init__(self, name, age, sex, year, mon, day) super(Teacher,self).__init__(name, age, sex, year, mon, day) self.level = level self.salary = salary def teaching(self): People.talking(self) print(‘%s is teaching‘ % self.name) class Student(People): def __init__(self, name, age, sex, year, mon, day, group): People.__init__(self, name, age, sex, year, mon, day) self.group = group def studying(self): People.talking(self) print(‘%s is studying‘ % self.name) t1 = Teacher(‘egon‘, 18, "男", "初级", 3000, 1990, 9, 10) print(t1.name, t1.age) t1.walking() t1.talking() s1 = Student(‘xiaobai‘, 22, "女", 2008, 1, 20, "一组") print(s1.name, s1.age) s1.talking() s1.walking()
类关系图:
birth是People类的一个实例属性,
birth的值为Date类的一个实例化对象,
故,Date类是People类的组成部分,属于组合关系。
延伸:类图生成工具:https://www.cnblogs.com/andy9468/p/8353613.html
原文地址:https://www.cnblogs.com/andy9468/p/9687986.html
时间: 2024-10-16 13:13:34