is-a就是对象和类之间通过类的关系相关联;继承
has-a是对象和类相关联是因为他们彼此参考。包含
is-a是三文鱼和鱼的关系;has-a是三文鱼和鳃的关系
总结:
记住!!一个新的顶级类必须继承object
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.youkuaiyun.com/johnsonguo/article/details/585193
即之前在一个类B中调用类A,如果改成调用类C修改代码量很大 然后用 super(B, self).__init__()就好。
## Animal is-a object (yes, sort of confusing) look at the extra credit class 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 Animal class 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 object class 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 object class Fish(object): pass ## Class Salmon is-a fish class Salmon(Fish): pass ## Class Halibut is-a Fish class Halibut(Fish): pass ## rover is-a Dog rover = Dog("Rover") ## satan is-a Cat satan = Cat("Satan") ## mary is-a Person mary = Person("Mary") ## mary has-a pet the pet has-a name satan???
##pet attribute of mary is-a sat????? why mary.pet = satan ## frank is a instance of Employee has-a name Frank and has-a salary 120000
## frank is-a Employee instance frank = Employee("Frank", 120000) ## frank has-a pet and the pet has-a name rover
## pet attribute of frank is-a rover frank.pet = rover ## flipper is-a instance of Fish
## flipper is Fish instance flipper = Fish() ## crouse is-a instance of Salmon
## crouse is Salmon instance crouse = Salmon() ## harry is-a instance of Halibut
## harry is Halibut instance harry = Halibut()
关系:
object -> Animal -> Dog -> rover('Rover')
-> lassie('Lassie')
...
...
-> Cat -> satan('Lucifer')
-> Person -> mary("Mary", pet=satan)
...