exercise 42 继承 包含 对象和类

本文介绍了面向对象编程的基本概念,包括类之间的继承关系(is-a)和组合关系(has-a),并通过具体的动物类实例展示了如何使用Python实现这些关系。同时,文中还解释了super函数的作用及其用法。

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.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)
      ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值