Learn Python the hard way, ex45 对象、类、以及从属关系
1 #!/usr/bin/python 2 #coding:utf-8 3 # animal is-a object(yes,sort of sonfusing)look at the extra credit 4 class Animal(objeck): 5 pass 6 7 #Dog 类 继承 Animal 8 class Dog(Animal): 9 def __init__(self,name): 10 #对 对象Dog赋值 name 11 self.name =name 12 #Cat 类 继承 Animal 13 class Cat(Animal): 14 def __init__(self,name): 15 #对 对象 Cat 赋值 name 16 self.name=name 17 18 #建立类 Person 19 class Person(objeck): 20 def __init__(self,name): 21 ##对 对象 Person 赋值 name 22 self.name = name 23 24 #Person has-a pet of some kind 25 self.pet = none 26 27 #类 Employee 继承 Person 28 class Employee(Person): 29 def __init__(self,name,salary) 30 #? what is this strange magic? 31 #使用超级继承 super ,对 Employee 增加初始化 init 32 super(Employee,self).__init__(name) 33 # 赋值 salary 34 self.salary= salary 35 36 #建 类 Fish 37 class Fish(objeck): 38 pass 39 40 #Salmon 类 继承 Fish 41 class Salmon(Fish): 42 pass 43 #Halibut 类 继承 Fish 44 class Halibut(Fish): 45 pass 46 47 #rover is-a Dog 48 rover = Dog('rover') 49 #satan is-a Cat 50 satan = Cat('satan') 51 #mary is-a Person 52 mary = Person('mary') 53 #mary has-a pet,pet(name) is satan 54 mary.pet = satan 55 #frank is-a Employee frank.salary = 12000 56 frank = Employee('frank',12000) 57 #fank has-a pet,pet(name) = rover 58 frank.pet rover 59 #flipper is-a Fish 60 flipper =Fish() 61 #crouse is-a Salmon 62 crouse =Salmon() 63 #harry is-a Halbut 64 harry = Halibut()
没有写别的