LearnPython3theHardWay__Excercise 44 Inheritance versus Composition

**ex40- 44都是类的知识 **

绝大多数继承都能简化成组合或者被组合替代,而且,避免使用多继承。

什么是继承?
当你编写 class Child(Parent)时,子类就继承了父类的属性和功能

class Parent(object):
	def implicit(self):		# implicit 隐性
		print("PARENT implicit()")

class Child(Parent):
	pass

dad = Parent()
son = Child()

dad.implicit()
son.implicit()

=========result=========
PARENT implicit()
PARENT implicit()

我们注意到,Child类下面并没有任何方法和属性,我们有son = Child()建立了个实例,Child还能继续工作,因为继承了Parent的方法。这是一种隐式继承。

有时候,你想要个性一些,不要执行父类的方法。只需要在子类下定义同名的方法即可

class Parent(object):
	def override(self):		# override 覆盖,重写
		print("PARENT override()")

class Child(Parent):
	def override(self):
		print("CHILD override()")

dad = Parent()
son = Child()

dad.override()
son.override()

==========result==========
PARENT override()
CHILD override()

还有一种特殊的继承。
您希望在执行子类的中途使用父类的方法来执行。您首先像在上一个示例中那样重写同名函数,然后使用名为super的python内置函数来调用的父类的方法。看下面的例子来理解:

class Parent(object):
	def altered(self):
		print("PARENT altered()")

class Child(Parent):
	def altered(self):
		print("CHILD, BEFORE PARENT altered()")	# 子类有altered(),先执行这一行
		super(Child,self).altered()	# 用super寻找Child的父类,并执行父类.altered()
		print("CHILD, AFTER PARENT altered()") # 上述执行完毕后,回来继续执行

dad = Parent()
son = Child()

dad.altered()
son.altered()

==========result==========
PARENT altered()
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()

综合以上,再写一段代码,理解它们:

class Parent(object):
	def override(self):
		print("PARENT override()")

	def implicit(self):
		print("PARENT implicit()")

	def altered(self):
		print("PARENT altered()")

class Child(Parent):
	def override(self):
		print("CHILD override()")
	def altered(self):
		print("CHILD, BEFORE PARENT altered()")
		super(Child,self).altered()
		print("CHILD, AFTER PARENT altered()")

dad = Parent()
son = Child()

dad.implicit()
son.implicit()

dad.override()
son.override()

dad.altered()
son.altered()

===========result==========
PARENT implicit()
PARENT implicit()
PARENT override()
CHILD override()
PARENT altered()
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()

为什么会有super()
类有多重继承的特性,如:

class SuperFun(Child, Badstuff):
	pass

SuperFun 同时继承了 ChildBadstuff 这两个类。当对任何SuperFun实例进行隐性操作时,python都要查找父类,按先后顺序调用(先Child后Badstuff)里面可能的方法。我们不用管是哪个父类,用super,python会自动寻找并为你调用父类方法
最常见的是跟__init__配合使用。

class Child(Parent):
	def __init__(self, stuff):
		self.stuff = stuff
		super(Child,self).__init__()

继承是很有用,但不依赖于隐性继承,通过创建其他的类和模块也能做到同样的事情。

class Other():
	def override(self):
		print("OTHER override()")
	def implicit(self):
		print("OTHER implicit()")
	def altered(self):
		print("OTHER altered()")

class Child():
	def __init__(self):
		self.other = Other()
	def implicit(self):
		self.other.implicit()
	def override(self):
		print("CHILD override()")
	def altered(self):
		print("CHILD, BEFORE OTHER altered()")
		self.other.altered()
		print("CHILD, AFTER OTHER altered()")

son = Child()

son.implicit()
son.override()
son.altered()

===========result============
OTHER implicit()
CHILD override()
CHILD, BEFORE OTHER altered()
OTHER altered()
CHILD, AFTER OTHER altered()

这个例子没有用继承。两个类的代码大部分相同


Study Drill

读下面的链接,相信没人会读
http://www.python.org/dev/peps/pep-0008/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To plot C versus S for 0 ≤ S ≤ 200, we can use the following MatLab code: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); C = zeros(size(S)); for i = 1:numel(S) C(i) = EuropeanCall(S(i), E, r, sigma, T); end plot(S, C); xlabel('S'); ylabel('C'); title('European Call Option Price'); ``` This code first defines the parameters of the option, and then generates a range of values for S between 0 and 200 using the `linspace` function. For each value of S, the code calculates the option price using the `EuropeanCall` function (which can be obtained from various sources). Finally, the code plots the option price as a function of S using the `plot` function. To plot the value of C(S, t) at different values of t between t = 0 and t = T, we can modify the above code as follows: ```matlab E = 100; % Exercise price T = 1; % Expiry date r = 0.05; % Risk-free interest rate sigma = 0.3; % Volatility S = linspace(0, 200, 1000); t = linspace(0, T, 1000); C = zeros(numel(S), numel(t)); for i = 1:numel(S) for j = 1:numel(t) C(i, j) = EuropeanCall(S(i), E, r, sigma, T - t(j)); end end surf(S, t, C); xlabel('S'); ylabel('t'); zlabel('C'); title('European Call Option Price'); ``` This code generates a grid of values for S and t using `linspace`, and then calculates the option price for each combination of S and t using a nested loop. The option price is stored in a matrix `C`, and is plotted as a surface using the `surf` function. As t approaches T, the option price converges to the intrinsic value of the option, which is max(S - E, 0) for a call option. This is because as the expiry date approaches, the option has less time to move in the money, and its time value decreases.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值