物件導向2,

來源:互聯網
上載者:User

物件導向2,
類的繼承

 1 class person(object): 2     def __init__(self,name,age): 3         self.name = name 4         self.age = age 5  6     def info(self): 7         print("the person's name is %s age is %d"%(self.name, self.age)) 8  9 class yello_person(person):  #繼承person類10     def __init__(self, name, age, level):11         person.__init__(self, name, age)  #建構函式先繼承父類,自己再增加屬性參數12         self.level = level13 14     def show_level(self):15         print("the person %s level is %d"%(self.name, self.level))16 17 y1 = yello_person('zsq', 28, 15)18 y1.info()  #使用繼承父類的方法19 y1.show_level()  #使用子類自己的方法

上面代碼中yello_person繼承了person父類。

子類中建構函式先對父類的建構函式進行繼承;然後加上自己的特有屬性。

18,19行樣本如何使用對象分別調用父類和子類的方法

 1 class SchoolMember(object): 2     Member = 0 3     def __init__(self, name, age): 4         self.name = name 5         self.age = age 6         SchoolMember.regist(self)  #建構函式中直接調用註冊方法,給成員數量加1 7     def regist(self): 8         SchoolMember.Member += 1  #操作公有屬性 9 10     def display(self):  #列印對象所有屬性的方法11         print('%s info is--'%self.name)12         for k,v in self.__dict__.items():13             print('\t', k, '\t',v)14         print('end ---')15 16     def __del__(self):  #解構函式,在手動刪除某個對象時會對該對象執行。或者整個程式結束時會對所有對象執行,此程式會在程式末尾列印。17         print('開除了%s'%self.name)18 19 class Teacher(SchoolMember):20     '''21     定義Teacher子類,在初始化時自動完成註冊功能。在繼承父類基礎上增加自己的屬性以及方法22     '''23     def __init__(self, name, age, salay, course):24         SchoolMember.__init__(self, name, age)25         self.salary = salay26         self.couesr = course27     def teaching(self):28         print("%s teaching %s"%(self.name, self.couesr))29 30 class Student(SchoolMember):31     def __init__(self, name, age, tuition ):32         SchoolMember.__init__(self, name, age)33         self.tuition = tuition34     def tuition(self):35         print('%s pay money %d for study'%(self.name, self.tuition))36 37 38 t1 = Teacher('zsq', 28, 15000, "cloud_compute")39 t1.teaching() #調用子類方法40 t1.display() #調用父類方法41 print(SchoolMember.Member) #列印學校成員,在對象初始化時自動加1

 

上面代碼中6行樣本如何在建構函式中操作公有屬性,實作類別似全域計數功能。

10行定義的方法樣本如何在父類中列印對象的所有屬性

 

多態

統一的調用介面,對不同對象有不同的處理邏輯。

 1 class Animal(object): 2     def talk(self): 3         print('this is father class') 4  5 class Dog(Animal): 6     def talk(self): 7         print("WangWang旺") 8  9 class Cat(Animal):10     def talk(self):11         print("喵喵喵")12 13 d1 = Dog()14 c1 = Cat()15 def animal_talk(obj):   #當前實現統一介面調用不同對象產生不同效果,即多態。可以使用樣本函數作為過渡方案16     obj.talk()   #此處obj為具體的對象,調用對象的talk方法。即子類中重構以後的方法,來實現多態。17 animal_talk(d1)18 animal_talk(c1)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.