組合||抽象類||多態||鴨子類型
阿新 • • 發佈:2018-03-19
name open class 重要 int AR 軟件 eache 添加屬性
組合
軟件重用的重要方式除了繼承之外還有另外一種方式,即:組合
組合指的是,在一個類中以另外一個類的對象作為數據屬性,稱為類的組合
class People: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex class Renwen(People): def __init__(self,name,age,sex,weapon): super().__init__(name,age,sex) self.weaponView Code= weapon self.attribute = [] #銳雯的屬性列表 def skill(self): print(‘%s is skill‘%self.name) class Teacher(People): def __init__(self,name,age,sex,level): super().__init__(name,age,sex) self.level = level def teach(self): print("%s is teaching" %self.name)class Attribute: def __init__(self,damage,spell,armor): self.danage = damage self.spell = spell self.armor =armor def tell_info(self): print("生命值:%s,法術:%s,護甲:%s"%(self.danage,self.spell,self.armor)) rewen = Renwen(‘銳雯‘,22,‘female‘,"砍刀") teacher1 =Teacher(‘林書行‘,30,‘male‘,20) a = Attribute(200,20,100)#創建屬性對象 rewen.attribute.append(a) #添加屬性值 #打印出屬性值 for i in rewen.attribute: i.tell_info()
組合||抽象類||多態||鴨子類型