回覆列表
  • 1 # 唯我曼聯

    # -*- coding: UTF-8 -*-

    #建立一個空類

    class Person():

    pass

    someone =Person()

    #加入初始化方法

    class Person():

    def __init__(self):#第一個引數必須為self

    pass

    #為初始化方法新增額外引數

    class Person():

    def __init__(self,name):#初始化時需要加入引數

    self.name = name

    #在函式內部,用self.name可以訪問name屬性,在外部就需要用透過例項化的物件名來訪問,如hunter.name

    hunter = Person("Elner Fudd")

    #step1:檢視Person類的定義

    #step2:在記憶體中例項化(建立)一個新的物件

    #step3:引數傳入

    #step4:name的值傳送給物件

    #step5:返回新的物件

    #step6:將物件與hunter關聯起來

    print hunter.name

    #類的繼承

    class Car():

    def exclaim(self):#self的意義,之後再寫

    print("i am a car")

    class Yugo(Car):

    pass

    #Yugo是Car的子類,繼承Car的方法和功能

    c = Car()

    y = Yugo()

    c.exclaim()

    y.exclaim()

    #覆蓋原方法

    class Car():

    def __init__(self,name):

    self.name = name

    def exclaim(self):

    print("i am " + self.name)

    class Yogu():

    def __init__(self,name):

    self.name = name + "!"

    def exclaim(self):

    print("i am " + self.name)

    class Motor():

    def __init__(self,name):

    self.name = name + "?"

    def exclaim(self):

    print("i am " + self.name)

    c = Car("Tom")

    y = Yogu("Jerry")

    m = Motor("Adams")

    print(c.name)

    print(y.name)

    print(m.name)

    c.exclaim()

    y.exclaim()

    m.exclaim()

    #為 子類 新增新方法

    class Car():

    def __init__(self,name):

    self.name = name

    class Yugo(Car):

    def __init__(self,name):

    self.name = name + "!"

    def need_a_push(self,who):

    print ("i need a push from" + who)

    y = Yugo("Daddy")

    print(y.name)

    y.need_a_push("Tom")

    #子類的方法,父類無法訪問

    #c = Car("name")

    #c.need_a_push("Tom")

    #"""Traceback (most recent call last):

    # File "/tmp/963057337/main.py", line 89, in <module>

    # c.need_a_push("Tom")

    #AttributeError: Car instance has no attribute "need_a_push""""

    #使用super()從父輩哪兒獲得幫助

    class Parent():

    def __init__(self,name):

    self.name = name

    def exclaim(self):

    print (self.name)

    class Kid(Parent):

    def __init__(self,name,gender):

    # super().__init__(name)#將self自動傳遞給父類,因此只用傳遞其餘引數

    self.name = name

    self.gender = gender

    def exclaim(self,who):

    super().exclaim()

    #同理,因為父類的exclaim方法只有self引數,所以不需要再傳入引數

    #但是要保證self.name屬性存在

    #相當於 def exclaim(self):

    # print(self.name)

    print("i am " + who)

    k = Kid("Alice","Famale")

    print(k.gender)

    k.exclaim("admas")

    **子類繼承父類方法的幾種形式

    class A:

    def method(self, arg):

    return

    class B(A):

    def method(self, arg):

    # A.method(self,arg) #1

    # super(B, self).method(arg) #2

    # super().method(arg) #3

    #python3 使用方法3取代了較複雜的方法2

    舉例來說:

    class A():

    def __init__(self,name,age):

    self.name =name

    self.age = age

    def get_name(self):

    print(self.name)

    def get_age(self):

    print(self.age)

    class B(A):

    def __init__(self,name,age):

    A.__init__(self,name,age) #1

    def get_name(self):

    super(B,self).get_name() #2

    def get_age(self):

    super().get_age()

    b = B("Alice",55)

    print(b.name)

    print(b.age)

    b.get_name()

    b.get_age()

  • 中秋節和大豐收的關聯?
  • 週五上證拉紅,有人說又可以找理由發新股了,對此你如何看?