中国高校课件下载中心 》 教学资源 》 大学文库

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

文档信息
资源类别:文库
文档格式:PPTX
文档页数:15
文件大小:638.54KB
团购合买:点击进入团购
内容简介
南京大学:《计算机程序的构造和解释 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

共15页,试读已结束,阅读完整版请下载
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档