exercise 42 继承 包含 对象和类

来源:互联网 发布:it就业培训学校 编辑:程序博客网 时间:2024/06/05 06:01

is-a就是对象和类之间通过类的关系相关联;继承

has-a是对象和类相关联是因为他们彼此参考。包含

is-a是三文鱼和鱼的关系;has-a是三文鱼和鳃的关系


总结:

记住!!一个新的顶级类必须继承object


super(Employee, self).__init__(name) 这个语句是干什么用的?
That's how you can run the __init__ method of a parent class reliably.
这样能可靠的运行父类中__init__函数

super用法官方说明:
super(type[, object-or-type])

  Return the superclass of type. If the second argument is omitted the super object
  returned is unbound. If the second argument is an object, isinstance(obj, type) 
  must be true. If the second argument is a type, issubclass(type2, type) must be 
  true. super() only works for new-style classes.

  A typical use for calling a cooperative superclass method is:

   class C(B):
       def meth(self, arg):
           super(C, self).meth(arg)

  New in version 2.2.


super 用法研究:

http://blog.csdn.net/johnsonguo/article/details/585193

即之前在一个类B中调用类A,如果改成调用类C修改代码量很大 然后用 super(B, self).__init__()就好。




## Animal is-a object (yes, sort of confusing) look at the extra creditclass Animal(object):    pass## Class Dog is-a Animal
## Dog is-a Animal (Animal is-a Object, so Dog is-a Object as well.)class Dog(Animal):    def __init__(self, name):        ## Dog has-a name  class Dog has a __init__ that takes self and name parameters        self.name = name## Class Cat is-a Animalclass Cat(Animal):    def __init__(self, name):        ## Cat has-a name  class Cat has a __init__ that takes self and name parameters        self.name = name## Class Person is-a objectclass Person(object):    def __init__(self, name):        ## Person has-a name   ##class Person has a __init__ that takes self and name parameters        self.name = name        ## Person has-a pet of some kind        self.pet = None## Class Employee is-a Person
## Employee is-a Person (and is-a Object, of course, everything is-a Object!)class Employee(Person):    def __init__(self, name, salary):        ##  Employee has-a name and salary ??? ?? hmm what is this strange magic?
  ##Return a proxy object that delegates method calls to a parent or sibling class of type
  ##This is a reliable way to call the __init__ method from Employee's 'super Class' Person (including name and pet)        super(Employee, self).__init__(name)        ## Employee has-a salary attribute        self.salary = salary## Class Fish is-a objectclass Fish(object):    pass## Class Salmon is-a fishclass Salmon(Fish):    pass## Class Halibut is-a Fishclass Halibut(Fish):    pass## rover is-a Dogrover = Dog("Rover")## satan is-a Catsatan = Cat("Satan")## mary is-a Personmary = Person("Mary")## mary has-a pet the pet has-a name satan???
##pet attribute of mary is-a sat????? whymary.pet = satan## frank is a instance of Employee has-a name Frank and has-a salary 120000
## frank is-a Employee instancefrank = Employee("Frank", 120000)## frank has-a pet and the pet has-a name rover
## pet attribute of frank is-a roverfrank.pet = rover## flipper is-a instance of Fish
## flipper is Fish instanceflipper = Fish()## crouse is-a instance of Salmon
## crouse is Salmon instancecrouse = Salmon()## harry is-a instance of Halibut
## harry is Halibut instanceharry = Halibut()
关系:
object -> Animal -> Dog -> rover('Rover')                 
       -> lassie('Lassie')  
      ...  
      ...    
                 -> Cat -> satan('Lucifer')
       -> Person -> mary("Mary", pet=satan)
      ...

0 0
原创粉丝点击