自學Python5.3-類和對象的簡單操做

自學Python之路html

自學Python5.3-類和對象的簡單操做

1. 定義一個類 

定義一個類的格式以下:python

class 類名:
    方法列表

 舉例1 : this

class Cat:    #定義一個Cat類
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")

 舉例2:spa

class Car:  # 定義一個Car類
    # 屬性
    # 方法
    def getCarInfo(self):
        print('車輪子個數:%d, 顏色%s'%(self.wheelNum, self.color))     # 稍後介紹
    def move(self):
        print("車正在移動...")

說明:

  • 定義類時有2種:新式類和經典類,上面的Car爲經典類,若是是Car(object)則爲新式類
  • 類名 的命名規則按照"大駝峯"

2. 建立對象

類不會自動執行,因而須要建立對象。3d

建立對象的格式爲: 指針

  對象名 = 類()  

 舉例1:htm

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
#建立一個對象
tom = Cat()

 舉例2:對象

class Car:  # 定義一個Car類
    # 屬性
    # 方法
    def getCarInfo(self):
        print('車輪子個數:%d, 顏色%s'%(self.wheelNum, self.color))     # 稍後介紹
    def move(self):
        print("車正在移動...")
#建立一個對象
BWM = Car()

3. 調用對象的方法

  舉例1 :blog

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
#建立一個對象
tom = Cat()
#調用tom指向的對象中的方法
tom.eat()
tom.drink()

4. 獲取對象添加屬性

  舉例1 :內存

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
#建立一個對象
tom = Cat()
#調用tom指向的對象中的方法
tom.eat()
tom.drink()
# 給tom指向的對象添加兩個屬性
tom.name = 「湯姆」
tom.age = 40
print("%的年齡是:%d"%(tom.name,tom.age))

  

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
    def introduce(self):
        print("%的年齡是:%d"%(tom.name,tom.age))
#建立一個對象
tom = Cat()
#調用tom指向的對象中的方法
tom.eat()
tom.drink()
# 給tom指向的對象添加兩個屬性
tom.name = 「湯姆」
tom.age = 40
#獲取屬性
tom.introduce()

5. 建立多個對象 

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
    def introduce(self):
        print("%的年齡是:%d"%(tom.name,tom.age))
#建立一個對象
tom = Cat()
#調用tom指向的對象中的方法
tom.eat()
tom.drink()
# 給tom指向的對象添加兩個屬性
tom.name = 「湯姆」
tom.age = 40
#獲取屬性
tom.introduce()
#建立另外一個對象
lanmao = Cat()
#調用lanmao指向的對象中的方法
lanmao.eat()
lanmao.drink()
# 給lanmao指向的對象添加兩個屬性
lanmao.name = 「藍貓」
lanmao.age = 12
#獲取屬性
tom.introduce()
lanmao.introduce()  

  這樣的結果確定不對,那是由於lanmao.introduce()調用內存的時候對應的是print("%的年齡是:%d"%(tom.name,tom.age)) ,那麼如何解決此問題???

class Cat:
    #屬性
    #方法
    def eat(self):
        print("貓在吃魚....")
    def drink(self):
        print("貓正在喝kele.....")
    def introduce(self):
        print("%的年齡是:%d"%(self.name,self.age))
#建立一個對象
tom = Cat()
#調用tom指向的對象中的方法
tom.eat()
tom.drink()
# 給tom指向的對象添加兩個屬性
tom.name = 「湯姆」
tom.age = 40
#獲取屬性
tom.introduce()
#建立另外一個對象
lanmao = Cat()
#調用lanmao指向的對象中的方法
lanmao.eat()
lanmao.drink()
# 給lanmao指向的對象添加兩個屬性
lanmao.name = 「藍貓」
lanmao.age = 12
#獲取屬性
tom.introduce()
lanmao.introduce()  

self總結:

  • 所謂的self,能夠理解爲本身
  • self就至關於一個變量, 能夠換成任何一個字符
  • 能夠把self當作C++中類裏面的this指針同樣理解,就是對象自身的意思
  • 某個對象調用其方法時,python解釋器會把這個對象做爲第一個參數傳遞給self,因此開發者只須要傳遞後面的參數便可

 

......

相關文章
相關標籤/搜索