Python物件導向的三大特點:封裝,繼承和多態(樣本)

來源:互聯網
上載者:User

標籤:sha   繼承   函數   結果   may   python面向對   abstract   orm   error   

繼承

單繼承:

#類定義class people:    #定義基本屬性    name = ‘‘    age = 0    #定義私人屬性,私人屬性在類外部無法直接進行訪問    __weight = 0    #定義構造方法    def __init__(self,n,a,w):        self.name = n        self.age = a        self.__weight = w    def speak(self):        print("%s 說: 我 %d 歲。" %(self.name,self.age)) #單繼承樣本class student(people):    grade = ‘‘    def __init__(self,n,a,w,g):        #調用父類的構函        people.__init__(self,n,a,w)        self.grade = g    #覆寫父類的方法    def speak(self):        print("%s 說: 我 %d 歲了,我在讀 %d 年級"%(self.name,self.age,self.grade))   s = student(‘ken‘,10,60,3)s.speak()

 實現結果

ken 說: 我 10 歲了,我在讀 3 年級

 多繼承:(雖然是可以的,但是不建議這麼做,只需要瞭解繼承時的順序是由左至右的即可)

#類定義class people:    #定義基本屬性    name = ‘‘    age = 0    #定義私人屬性,私人屬性在類外部無法直接進行訪問    __weight = 0    #定義構造方法    def __init__(self,n,a,w):        self.name = n        self.age = a        self.__weight = w    def speak(self):        print("%s 說: 我 %d 歲。" %(self.name,self.age)) #單繼承樣本class student(people):    grade = ‘‘    def __init__(self,n,a,w,g):        #調用父類的構函        people.__init__(self,n,a,w)        self.grade = g    #覆寫父類的方法    def speak(self):        print("%s 說: 我 %d 歲了,我在讀 %d 年級"%(self.name,self.age,self.grade)) #另一個類,多重繼承之前的準備class speaker():    topic = ‘‘    name = ‘‘    def __init__(self,n,t):        self.name = n        self.topic = t    def speak(self):        print("我叫 %s,我是一個演說家,我演講的主題是 %s"%(self.name,self.topic)) #多重繼承class sample(speaker,student):    a =‘‘    def __init__(self,n,a,w,g,t):        student.__init__(self,n,a,w,g)        speaker.__init__(self,n,t) test = sample("Tim",25,80,4,"Python")test.speak()   #方法名同,預設調用的是在括弧中排前地父類的方法

 封裝:

class Student(object):    def __init__(self, name, score):        self.name = name         self.score = scoreMay = Student("May",90)                      # 須要提供兩個屬性Peter = Student("Peter",85)print(May.name, May.score)print(Peter.name, Peter.score)def print_score(Student):                    # 外部函數    print_score(Student)# print("%s‘s score is: %d" %(Student.name,Student.score))             # 普通 print 寫法print("{0}‘s score is: {1}".format(Student.name,Student.score))        print_score(May)    print_score(Peter)

 多態:

class Animal(object):    def __init__(self, name):  # Constructor of the class        self.name = name    def talk(self):        raise NotImplementedError("Subclass must implement abstract method")class Cat(Animal):    def talk(self):        print(‘%s: 喵喵喵!‘ %self.name)class Dog(Animal):    def talk(self):        print(‘%s: 汪!汪!汪!‘ %self.name)def func(obj): #一個介面,多種形態    obj.talk()c1 = Cat(‘毛毛‘)d1 = Dog(‘灰灰‘)func(c1)func(d1)

 

Python物件導向的三大特點:封裝,繼承和多態(樣本)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.