# -*- coding:utf-8 -*- ## Animal is-a object (yes,sort of confusing) look at the extra credit class Animal(object): pass ## 添加在动物的子类狗?? class Dog(Animal): def __init__ (self,name): self.name = name print(self.name) ## 添加在动物的子类猫?? class Cat(Animal): def __init__ (self,name): self.name = name print(self.name) ## 添加人类?? class Person(object): def __init__(self,name): self.name = name ## person has-a pet of some kind self.pet = None #确保类中的self.pet属性设置为None print (self.name) ##添加劳动者?? class Employee(Person): def __init__(self, name, salary): ## ?? hmm what is this strange magic? Person.__init__(self, name) #super(Employee,self).__init__(name) 注意super函数的用法,和上一句代码作用相同 ##?? self.salary =salary print (self.salary) class Fish(object): pass ##在鱼的大类中定义salmon这个小类 ?? class salmon(Fish): pass ## 在鱼的大类中定于比目鱼这个小类?? class Halibut(Fish): pass ## 狗类的一个特定对象rover,rover is-a Dog rover = Dog ("ROVER") ## 猫类的一个特定对象satan?? satan = Cat("satan") ##人类的一个具体的对象mary?? mary = Person("Mary") ##mary的宠物satan??? mary.pet = satan ##劳动者的一个具体对象frank?? frank = Employee("Frank", 12000) ##frank的宠物??? frank.pet = rover ## flipper是鱼类的一个具体对象?? flipper = Fish() ## crourse 是salmon中的一个具体对象?? crouse = salmon() ##harry 是Halibut中的一个具体对象 ?? harry = Halibut()
时间: 2024-11-08 00:35:40