| 章節(jié)號 | 內容???????????? |
|---|---|
| 1圖片格式(png) | 寬度大于620px,保持高寬比減低為620px |
| 1-1 | 應用 |
| 1-1-1 | 方法 |
??VScode按Ctrl+↓,直接跳到下一行編輯
第1章節(jié)?類的初步(經(jīng)典類)
??類的基本結構:
calss 類名:
方法
-
1-1?類的初步—定義一個類并創(chuàng)建對象實例
class Car:
def move(self):
print("The car is moving")
car1 = Car()
car1.move()
li@li-ThinkPad-T420s:~/Desktop/py$ The car is moving
??1、類名car后必須要加冒號:
??2、方法中都必須加上self參數(shù),同時別忘記冒號:
??3、創(chuàng)建對象實例,使用對象名=類名()方式
??4、使用對象名.move()方式來使用類的move(self)函數(shù)(方法)
??1-1-1. 類的初步—定義一個類—為類的對象實例添加屬性
car1.color = "red"
car1.brand = "benz"
print(car1.color)
print(car1.brand)
li@li-ThinkPad-T420s:~/Desktop/py$ red
li@li-ThinkPad-T420s:~/Desktop/py$ benz
??1-1-2.類的初步—定義一個類—在類中使用屬性
class Car:
def move(self):
print("The car is moving")
def changBlack(self):
self.color="black"
car1 = Car()
car1.move()
car1.color = "red"
car1.brand = "benz"
print(car1.color)
print(car1.brand)
car1.changBlack()
print(car1.color)
The car is moving
red
benz
black
??1-1-3.類的初步—定義一個類—在類中初始化屬性
class Car:
def move(self):
print("The car is moving")
def changBlack(self):
self.color="black"
#關鍵在此方法,類似于C++的構造函數(shù)
def __init__(self):
self.color = "red"
self.brand = "benz"
car1 = Car()
car1.move()
#car1.color = "red"
#car1.brand = "benz"
print(car1.color)
print(car1.brand)
car1.changBlack()
print(car1.color)
The car is moving
red
benz
black
??這樣存在的問題是:我基于此類所產(chǎn)生的對象,全部都是red和benz。如何基于不同的需要,產(chǎn)生不同的color和brand呢?
??1-1-4.類的初步—定義一個類—使用參數(shù)來初始化不同對象的屬性
class Car:
def move(self):
print("The car is moving")
def changBlack(self):
self.color="black"
#在此函數(shù)中做手腳,加入兩個參數(shù),并把參數(shù)的值賦給color和brand
def __init__(self,colorP,brandP):
self.color = colorP
self.brand = brandP
#創(chuàng)建對象時直接把color和brand指明
car1 = Car("red","benz")
car1.move()
print(car1.color)
print(car1.brand)
car1.changBlack()
print(car1.color)
??注意一個問題,這里我們并沒有顯式的調用__init__()方法,參數(shù)也沒有傳遞給__init__(),而是直接放在了類的()括號中。當我們這樣做的時候,在創(chuàng)建對象的時候python會把參數(shù)傳遞給__init__(),并且自動調用__init__(),這是python內部機理。
The car is moving
red
benz
black
??1-1-5.類的初步—定義一個類—使用參數(shù)來修改屬性
class Car:
def move(self):
print("The car is moving")
#在此做手腳
def changBlack(self, colorC):
self.color = colorC
def __init__(self, colorP, brandP):
self.color = colorP
self.brand = brandP
car1 = Car("red", "benz")
car1.move()
print(car1.color)
print(car1.brand)
#調用時直接傳入`color`即可
car1.changBlack("black")
print(car1.color)
The car is moving
red
benz
black
??1-1-5.類的初步—定義一個類—把外部函數(shù)全部集成到類中
class Car:
def move(self):
print("The car is moving")
def changBlack(self, colorC):
self.color = colorC
def __init__(self, colorP, brandP):
self.color = colorP
self.brand = brandP
#添加一個方法,把print函數(shù)直接放在了類中
def printAttr(self):
print(self.color)
print(self.brand)
car1 = Car("red", "benz")
car1.move()
#直接調用對象的方法
car1.printAttr()
car1.changBlack("black")
#直接調用對象的方法
car1.printAttr()
The car is moving
red
benz
black
benz
-
1-2?類的初步—把對象當做參數(shù)進行傳遞
??剛才我們把全部外部的函數(shù)(實際也就只有print)全部集成到了類定義當中,類定義體之外全部都是對象名.的方式來調用方法。
??現(xiàn)在我們在類定義之外,定義一個函數(shù):
class Car:
def move(self):
print("The car is moving")
def changBlack(self, colorC):
self.color = colorC
def __init__(self, colorP, brandP):
self.color = colorP
self.brand = brandP
def printAttr(self):
print(self.color)
print(self.brand)
#定義一個外部(相對于類的內部而言)函數(shù),函數(shù)有一個obj參數(shù),函數(shù)體內obj調用了類的內部方法
def outFunction(obj):
obj.printAttr()
car1 = Car("red", "benz")
#調用時傳遞了一個對象給outFunction
outFunction(car1)
red
benz
??突然想到的一個問題,證明不同對象之間的的屬性是獨享,方法是共享:
class Car:
def move(self):
print("The car is moving")
def changBlack(self, colorC):
self.color = colorC
def __init__(self, colorP, brandP):
self.color = colorP
self.brand = brandP
def printAttr(self):
print(self.color)
print(self.brand)
def outFunction(obj):
obj.printAttr()
print(id(obj))
print(id(obj.printAttr))
car1 = Car("red", "benz")
outFunction(car1)
car2 = Car("blue", "bmw")
outFunction(car2)
red
benz
140134156651824
140134156412744
blue
bmw
140134156651992
140134156412744
??1-2-1. 類的初步—把對象當做參數(shù)進行傳遞—self是什么
??當一個類的對象實例化的時候(如下圖):
car1 = Car("red", "benz")
??python將為car1分配一片地址空間,并將該空間的地址傳遞給類的__init__方法中的self
def __init__(self, colorP, brandP):
self.color = colorP
self.brand = brandP
??這樣就將地址(即代表car1)與color和brand相結合。誰調用了方法,即who.printAttr(self),self==who!
-
1-3?類的初步—魔法方法(類似于 __name__() )
??首先我們看如果直接打印一個對象會發(fā)生什么:
car1 = Car("red", "benz")
print(car1)
<__main__.Car object at 0x7f1f5d8c9be0>
??可以看到輸出的是一個地址,但是這個地址的輸入似乎對程序的使用者沒有太大的意義,那么如何能在直接打印一個對象的時候輸入一些有意義的信息呢?
??我們可以定義一個__str__()方法來完成:
def __str__(self):
return ("i am "+self.brand)
car1 = Car("red", "benz")
print(car1)
i am abenz
??retuen 什么內容,打印對象就顯示什么內容
第2章節(jié)?類的初步之編程應用:游戲編寫
-
1-1?類的初步之編程應用—游戲編寫
第3章節(jié)?類的初步之編程應用:游戲編寫
??貼一段為學習而寫的代碼,方便以后繼續(xù)改進修改(使用VScode)。知識點:對象中再保存其他對象。其實保存的只是地址,而不是全部數(shù)據(jù)。
class Home:
def __init__(self, mianji):
self.mianji = mianji
self.jiajuList = []
def __str__(self):
jiaju = ""
for t in self.jiajuList:
if jiaju == "":
jiaju = t.leixing
else:
jiaju = jiaju+", "+t.leixing
jiaju = jiaju + "(占地面積"+str(t.mianji)+"平方米)"
msg = "我的家面積是120平方米,放了家具還有"+str(self.mianji)+"平方米的空間===.現(xiàn)在的家具有:"+jiaju
return msg
def banjiaju(self, obj):
if (self.mianji >= obj.mianji):
self.jiajuList.append(obj)
self.mianji = self.mianji - obj.mianji
else:
print("放不下了")
class Bed:
def __init__(self, mianji, leixing):
self.mianji = mianji
self.leixing = leixing
def __str__(self):
msg = "床的面積是:"+str(self.mianji)+".類型是:"+self.leixing
return msg
home = Home(120)
bed = Bed(20, "ximengsi")
print(home)
print(bed)
home.banjiaju(bed)
print(home)
bed2 = Bed(18, "gangsichaung")
print(bed2)
home.banjiaju(bed2)
print(home)
bed3 = Bed(83, "taikongchuang")
print(bed3)
home.banjiaju(bed3)
print(home)
我的家面積是120平方米,放了家具還有120平方米的空間===.現(xiàn)在的家具有:
床的面積是:20.類型是:ximengsi
我的家面積是120平方米,放了家具還有100平方米的空間===.現(xiàn)在的家具有:ximengsi(占地面積20平方米)
床的面積是:18.類型是:gangsichaung
我的家面積是120平方米,放了家具還有82平方米的空間===.現(xiàn)在的家具有:ximengsi(占地面積20平方米), gangsichaung(占地面積18平方米)
床的面積是:83.類型是:taikongchuang
放不下了
我的家面積是120平方米,放了家具還有82平方米的空間===.現(xiàn)在的家具有:ximengsi(占地面積20平方米), gangsichaung(占地面積18平方米)