python中類的學習筆記(源碼版)

來源:互聯網
上載者:User

標籤:類的使用

1.1第一段代碼

#定義一個類(define a class )class Cat:        #屬性(attribution)        #方法(methods)        def eat(self):                print("cat is eating a fish.")        def drink(slef):                print("cat is drinking milk.")        def introduce(self):                print("%s‘s age is %d"%(tom.chinese_name,tom.age))#建立一個對象(creating an object)tom = Cat()#調用一個對象的方法(method to invoke an object)tom.eat()tom.drink()#蠢辦法添加屬性(stupid method to add attributions)tom.chinese_name = "湯姆"tom.age = 18#擷取對象的屬性(the first way to get  properties of objects )tom.introduce()#建立多個對象,這裡建立第二個對象blue_cat(create multiple objects,and the second creates bule cat)blue_cat = Cat()blue_cat.chinese_name = "藍貓"blue_cat.age = 8blue_cat.introduce()

1.2執行第一段代碼的輸出

[[email protected] class]# python test.py cat is eating a fish.cat is drinking milk.湯姆‘s age is 18湯姆‘s age is 18    ‘‘‘那這裡我們可以看到兩個不同的對象調用方法後輸出了相同的結果,這就出現問題了,那怎麼解決上面的問題,且看下面的代碼。‘‘‘

2.1第2種方式
self的加入,解決了上面的問題。

#定義一個類(define a class )class Cat:        #屬性(attribution)        #方法(methods)        def eat(self):                print("cat is eating a fish.")        def drink(slef):                print("cat is drinking milk.")        def introduce(self):                print("%s‘s age is %d."%(self.chinese_name,self.age))#建立一個對象(creating an object)tom = Cat()#調用一個對象的方法(method to invoke an object)tom.eat()tom.drink()#蠢辦法添加屬性(stupid method to add attributions)tom.chinese_name = "湯姆"tom.age = 18#擷取對象的屬性(the first way to get  properties of objects )tom.introduce()#建立多個對象,這裡建立第二個對象blue_cat(create multiple objects,and the second creates bule cat)blue_cat = Cat()blue_cat.chinese_name = "藍貓"blue_cat.age = 8blue_cat.introduce()

2.2第二段代碼的輸出

[[email protected] class]# python test.py cat is eating a fish.cat is drinking milk.湯姆‘s age is 18.藍貓‘s age is 8.

3 init和str的使用

class Cat:        """定義了一個Cat類"""        #初始化對象        def __init__(self, new_name, new_age):                self.name = new_name                self.age = new_age        def __str__(self):            return "%s的年齡是:%d"%(self.name,self.age)        #方法        def eat(self):                print("貓在吃魚....")        def drink(self):                print("貓正在喝牛奶.....")        def introduce(self):                print("%s的年齡是:%d"%(self.name, self.age))#建立一個對象tom = Cat("湯姆", 40)bule_cat = Cat("藍貓", 10)print(tom)print(bule_cat)[[email protected] class]# python test2.py 湯姆的年齡是:40藍貓的年齡是:10

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.