
上QQ阅读APP看书,第一时间看更新
3.2.7 类的其他操作
类的实例也可以看作一种数据类型。类的实例也可以作为列表、元组、集合的元素,或字典的值,甚至作为函数的返回值。也可以在类中引用其他类的实例。
1.对象作为列表、元组、字典和集合的元素
下面的程序先创建一个student类,然后把学生信息赋予学生对象temp,并把对象temp加入列表和字典中,最后创建有学生对象的元组和集合。

2.对象作为属性值和函数返回值
下面的程序先创建person类,然后用person作为父类创建student和teacher类,在teacher类中创建student的对象,并把student对象加入person的私有列表self.__ myStudent中,通过teacher类的查询函数,返回学生对象。


