南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance

Inheritance 7/23/2019 Alex Stennet
Inheritance 7 / 23 / 2019 Alex Stennet

Review
Review

WWPD >>>class A: >>>a.X=5 ×=10 >>>A.X def __init__(self,y): 18 self.y =y >>>b.x >>a=A(4) 18 >>>b=A(6) >>>A.X=15 >>>a.X 5 >>>b.X 15
WWPD >>> class A: ... x = 10 ... def __init__(self, y): ... self.y = y ... >>> a = A(4) >>> b = A(6) >>> a.x = 5 >>> A.x >>> b.x >>> A.x = 15 >>> a.x >>> b.x 10 10 5 15

Re-Implement Rationals ●Rational Class o Attributes: ■Numerator 题Denominator o Methods: print()-should print 'numerator denominator' add(other)-should return a new rational with addition of current and other mul(other)-should return a new rational with multiplication of current and other
Re-Implement Rationals ● Rational Class ○ Attributes: ■ Numerator ■ Denominator ○ Methods: ■ print() - should print ‘numerator / denominator’ ■ add(other) - should return a new rational with addition of current and other ■ mul(other) - should return a new rational with multiplication of current and other

Inheritance
Inheritance

An energizing example class Pokemon: class ElectricType: """A Pokemon.""" """An electric Pokemon."" Al1 Pokemon have: Electric-type Pokemon have: ●a name ●a name ●a trainer ●a trainer ●a1eve1 ● a level ·an amount of HP(life) ●an amount of HP(life) ●an attack:tackle an attack:thunder shock Pokemon can: Electric-type Pokemon can: ●say their name ●say their name ●attack other Pokemon attack and sometimes paralyze receive damage other Pokemon ●receive damage
An energizing example class Pokemon: """A Pokemon.""" class ElectricType: """An electric Pokemon.""" All Pokemon have: ● a name ● a trainer ● a level ● an amount of HP (life) ● an attack: tackle Pokemon can: ● say their name ● attack other Pokemon ● receive damage Electric-type Pokemon have: ● a name ● a trainer ● a level ● an amount of HP (life) ● an attack: thunder shock Electric-type Pokemon can: ● say their name ● attack and sometimes paralyze other Pokemon ● receive damage

class Pokemon: basic_attack ='tackle' damage =40 def __init__(self,name,trainer): self.name,self.trainer name,trainer self.level,self.hp 1,50 self.paralyzed False def speak(self): print(self.name +!' def attack(self,other): if not self.paralyzed: self.speak() print(self.name,'used',self.basic_attack,!') other.receive_damage(self.damage) def receive_damage(self,damage): self.hp max(0,self.hp -damage) if self.hp ==0: print(self.name,fainted!')
class Pokemon: basic_attack = 'tackle' damage = 40 def __init__(self, name, trainer): self.name, self.trainer = name, trainer self.level, self.hp = 1, 50 self.paralyzed = False def speak(self): print(self.name + '!') def attack(self, other): if not self.paralyzed: self.speak() print(self.name, 'used', self.basic_attack, '!') other.receive_damage(self.damage) def receive_damage(self, damage): self.hp = max(0, self.hp - damage) if self.hp == 0: print(self.name, 'fainted!')

class ElectricType: basic-attack =thunder shock' .damage 40: prob 0.1 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 def__init__(self,name,trainer): self.name,self.trainer name,trainer self.level,self.hp 1,50 self.paralyzed False def speak(self): print(self.name +'!' def attack(self,other): self.speak() print(self.name,'used',self.basic_attack,'!') other.receive_damage(self.damage) if random()<self.prob and type(other)!=ElectricType: other.paralyzed True print(other.name,'is paralyzed!') ▣····年·▣4·4·…···年▣·▣4··4·…4g def receive_damage(self,damage): This is a lot of self.hp max(0,self.hp -damage): repeated code!: if self.hp =0: print(self.name,'fainted!')
class ElectricType: basic_attack = 'thunder shock' damage = 40 prob = 0.1 def __init__(self, name, trainer): self.name, self.trainer = name, trainer self.level, self.hp = 1, 50 self.paralyzed = False def speak(self): print(self.name + '!') def attack(self, other): self.speak() print(self.name, 'used', self.basic_attack, '!') other.receive_damage(self.damage) if random() < self.prob and type(other) != ElectricType: other.paralyzed = True print(other.name, 'is paralyzed!') def receive_damage(self, damage): self.hp = max(0, self.hp - damage) if self.hp == 0: print(self.name, 'fainted!') This is a lot of repeated code! :(

Class Relationships Electric-type Pokemon are simply specialized versions of regular Pokemon! basic_attack damage __init__ speak attack receive_damage basic_attack swim fly basic_attack prob attack
Class Relationships Electric-type Pokemon are simply specialized versions of regular Pokemon! basic_attack damage __init__ speak attack receive_damage basic_attack swim fly basic_attack prob attack

Using Inheritance We can implement specialized classes using inheritance! The class definition allows us to specify that a new class inherits from a superclass: class (): We call the more specialized class a subclass of the general class and the general class a superclass of the specialized class. The subclass inherits all class attributes of the superclass. The subclass can override attributes to specify how it is different from the superclass. Demo
Using Inheritance We can implement specialized classes using inheritance! The class definition allows us to specify that a new class inherits from a superclass: class (): We call the more specialized class a subclass of the general class and the general class a superclass of the specialized class. The subclass inherits all class attributes of the superclass. The subclass can override attributes to specify how it is different from the superclass. Demo
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)01 绪论 Introduction(主讲:曹洋).pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)02 傅里叶分析与卷积 Fourier Analysis and Convolution.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)03 数字图像处理基础 Basics of Digital Image Processing.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)04 图像模型 Basics of Image.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)05 空域滤波 Spatial Filtering.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)06 小波变换 Wavelet Analysis.pptx
- CodeIgniter 中国开发者社区:CodeIgniter4 中文手册(版本 4.0.0).pdf
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)07 图像复原 Image Restoration.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)08 自适应滤波 Adaptive Filter.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)要点复习 Review(主讲:曹洋).pptx