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
- exercise 42 继承 包含 对象和类
- Exercise 42: is-a ,has-a ,对象和类
- Learn Python The Hard Way学习(42) - 继承,包含,对象和类
- Exercise 40: 模块,类和对象
- 私有继承 和 包含类
- 对象和类继承
- 类和对象 -----继承
- 类和对象:继承
- Notes类的继承和包含
- Notes类的继承和包含
- C++继承和包含
- exercise 40 模块 类 对象
- 类继承和对象组合
- javascript类对象和继承
- 类和对象与继承
- 继承和派生--包含子对象的子类的构造函数
- 包含对象成员的类 vs普通公有继承的派生类 and 对象成员vs私有继承
- 类和对象、封装和继承
- 使用JSP做购物车一(实现)
- 由沉浸式状态栏引发的血案
- android OutputStream向sd卡中文件写入操作立即关机数据清0问题
- boost::filesystem指南
- Shader(着色器)相关介绍
- exercise 42 继承 包含 对象和类
- SSH网上商城--问题汇总
- JAVA之继承
- 2016 Android App必知必会
- 递归。。爬楼梯
- 1.23
- zookeeper清理日志
- sql的nvl用法
- 开源第三方登录组件OAuthLogin2.0 架构解析及开源地址