與小卡特一起學python 第14章 物件
#14章 物件
#14-1 建立一個簡單的Ball類
class Ball: #告訴python我們建立一個類
def bounce(self):
if self.direction == "down":
self.direction = "up"
#14-2 使用Ball類
class Ball:
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball()
myBall.direction = "down"
myBall.color = "red"
myBall.size = "small"
print("I just created a ball.")
print("My ball is",myBall.size)
print("My ball is",myBall.color)
print("My ball's direction is",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is",myBall.direction)
#14-3 增加一個__init__的方法傳遞引數
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball ("red","small","down")
print("I just created a ball.")
print("My ball is",myBall.color) #書中程式碼是size,實際應該是color
print("My ball is",myBall.size)#書中程式碼是color,實際應該是size
print("My ball's direction is ",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is ",myBall.direction)
#14-4 使用__str__()改變列印物件的方式
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def __str__(self):
msg = ("Hi,I'm a " + self.size + " " + self.color + " ball!")
return msg
myBall = Ball("red","small","down")
print (myBall)
#14-5 熱狗程式的開始部分
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
myDog = HotDog()
print(myDog.cooked_level)
print(myDog.cooked_string)
print(myDog.condiments)
print("Now I'm going o cook the hot dog")
myDog.cook(4)
print (myDog.cooked_level)
print (myDog.cooked_string)
#14-6 包含cook() add_condiments() __str__() HotDog
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def __str__(self):
msg = "hot dog"
if len(self.condiments) > 0:
msg = msg + "with"
for i in self.condiments:
msg = msg + i + ","
msg = msg.strip(",")
msg = self.cooked_string + "" +msg + "."
return msg
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
def addCondiment(self,condiment):
self.condiments.append(condiment)
myDog = HotDog()
print(myDog)
print("Cooking hot dog for 4 minutes...")
myDog.cook(4)
print(myDog)
print("Cooking hot dog for 3 more minutes...")
myDog.cook(3)
print(myDog)
print("What happens if I cook it for 10 more minutes?")
myDog.cook(10)
print(myDog)
print("Now , I'm going to add some stuff on my hot dog")
myDog.addCondiment("ketchup")
myDog.addCondiment("mustard")
print(myDog)
#14.8多型和繼承
class Triangle:
def __init__(self,width,height):
self.width = width
self.height =height
def getArea(self):
area = self.width * self.height / 2.0
return area
class Square:
def __init__(self,size):
self.size = size
def getArea(self):
area = self.size * self.size
return area
myTriangle = Triangle(4,5)
mySquare = Square(7)
print(myTriangle.getArea())
print(mySquare.getArea())
class Game_object:
def __init__(self,name):
self.name = name
def pickUp(self):
pass
#put code here to add the object
#to the player's collection
class Coin(Game_object):
def __init__(self,value):
GameObject.__init__(self,"coin")
self.value = value
def spend(selt,buyer,seller):
pass
#14-1 建立一個簡單的Ball類
class Ball: #告訴python我們建立一個類
def bounce(self):
if self.direction == "down":
self.direction = "up"
#14-2 使用Ball類
class Ball:
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball()
myBall.direction = "down"
myBall.color = "red"
myBall.size = "small"
print("I just created a ball.")
print("My ball is",myBall.size)
print("My ball is",myBall.color)
print("My ball's direction is",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is",myBall.direction)
#14-3 增加一個__init__的方法傳遞引數
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball ("red","small","down")
print("I just created a ball.")
print("My ball is",myBall.color) #書中程式碼是size,實際應該是color
print("My ball is",myBall.size)#書中程式碼是color,實際應該是size
print("My ball's direction is ",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is ",myBall.direction)
#14-4 使用__str__()改變列印物件的方式
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def __str__(self):
msg = ("Hi,I'm a " + self.size + " " + self.color + " ball!")
return msg
myBall = Ball("red","small","down")
print (myBall)
#14-5 熱狗程式的開始部分
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
myDog = HotDog()
print(myDog.cooked_level)
print(myDog.cooked_string)
print(myDog.condiments)
print("Now I'm going o cook the hot dog")
myDog.cook(4)
print (myDog.cooked_level)
print (myDog.cooked_string)
#14-6 包含cook() add_condiments() __str__() HotDog
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def __str__(self):
msg = "hot dog"
if len(self.condiments) > 0:
msg = msg + "with"
for i in self.condiments:
msg = msg + i + ","
msg = msg.strip(",")
msg = self.cooked_string + "" +msg + "."
return msg
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
def addCondiment(self,condiment):
self.condiments.append(condiment)
myDog = HotDog()
print(myDog)
print("Cooking hot dog for 4 minutes...")
myDog.cook(4)
print(myDog)
print("Cooking hot dog for 3 more minutes...")
myDog.cook(3)
print(myDog)
print("What happens if I cook it for 10 more minutes?")
myDog.cook(10)
print(myDog)
print("Now , I'm going to add some stuff on my hot dog")
myDog.addCondiment("ketchup")
myDog.addCondiment("mustard")
print(myDog)
#14.8多型和繼承
class Triangle:
def __init__(self,width,height):
self.width = width
self.height =height
def getArea(self):
area = self.width * self.height / 2.0
return area
class Square:
def __init__(self,size):
self.size = size
def getArea(self):
area = self.size * self.size
return area
myTriangle = Triangle(4,5)
mySquare = Square(7)
print(myTriangle.getArea())
print(mySquare.getArea())
class Game_object:
def __init__(self,name):
self.name = name
def pickUp(self):
pass
#put code here to add the object
#to the player's collection
class Coin(Game_object):
def __init__(self,value):
GameObject.__init__(self,"coin")
self.value = value
def spend(selt,buyer,seller):
pass
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2076518/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 與小卡特一起學python 第14章 物件 動手試一試Python物件
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第16章 圖形 Pygame學習PythonGAM
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第17章動畫精靈和碰撞檢測Python動畫
- 與小卡特一起學python 第18章 一種新的輸入-事件Python事件
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第10章 遊戲時間到了 程式碼清單Python遊戲
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- 與小卡特一起學python 第1章 出發吧 1-1練習我們第一個真正的程式Python
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學python 第6章 gui-圖形使用者介面 6-1-2-3-4-5 gui使用PythonGUI
- 第14章PhpDigPHP
- 第 14 章 CSS 顏色與度量單位CSS
- 第14章. 標準元素 (轉)
- 《父與子的程式設計之旅(第3版)》第14章習題答案程式設計
- JavaScript物件程式設計-第3章JavaScript物件程式設計
- 第2章 Python序列Python
- 《UML物件導向設計基礎》—第2章2.5節本章小結物件