类的组合

类的组合

组合:一个类的对象是另外一个类对象的属性

# 学生类
    # 姓名 性别 年龄 学号 班级 手机号
class Student:
    def __init__(self,name,sex,age,number,clas,phone):
        self.name = name
        self.sex = sex
        self.age = age
        self.clas =clas
        self.phone = phone
# 班级信息
    # 班级名字
    # 开班时间
    # 当前讲师
class Clas:
    def __init__(self,cname,begint,teacher):
        self.cname = cname
        self.begint = begint
        self.teacher = teacher

# 查看的是雪飞的班级的开班日期是多少
py22 = Clas('python22期','2019-6-22','小白')   #***
xf = Student('雪飞','male',18,101,py22,13899566666)
dz = Student('大壮','male',18,102,'py23',13689995741)
print(xf.__dict__)
# {'name': '雪飞', 'sex': 'male', 'age': 18, 'clas': <__main__.Clas object at 0x0000000002897388>, 'phone': 13899566666}
print(dz.__dict__)
# {'name': '大壮', 'sex': 'male', 'age': 18, 'clas': 'py23', 'phone': 13689995741}
print(xf.clas.__dict__)
# {'cname': 'python22期', 'begint': '2019-6-22', 'teacher': '小白'}
print(xf.clas,py22)
#同一地址:<__main__.Clas object at 0x0000000002897388> <__main__.Clas object at 0x0000000002897388>
print(xf.clas.begint)
#2019-6-22
原文地址:https://www.cnblogs.com/wyh0717/p/13159263.html