與小卡特一起學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物件導向思想(類與物件)Python物件
- 【python 物件導向】 python物件學習筆記《1》Python物件筆記
- (003)我們一起學Python;鞏固練習,寫個小遊戲Python遊戲
- 小陳學JS js內建物件 Date物件JS物件
- Python中的類與物件Python物件
- Python基礎-類與物件Python物件
- 與 MDN 一起學習 JavaScriptJavaScript
- 小小白Python入門,陪你一起學PythonPython
- python的不可變物件與可變物件及其妙用與坑Python物件
- Python學習第6天Python
- Python 簡明教程 --- 19,Python 類與物件Python物件
- Python之物件導向基礎小練Python物件
- Python小知識之物件的比較Python物件
- Python學習筆記 9.0 模組 與 包 與 物件導向版學員管理系統Python筆記物件
- 草根學Python(九) 物件導向Python物件
- 有沒有一起學 Laravel 教程的小夥伴!!!Laravel
- Python學習筆記|Python之物件導向Python筆記物件
- 第 六小節 常量與列舉
- python中物件導向_類_物件的概念與定義Python物件
- python學習筆記:第6天 小資料池和編碼轉換Python筆記
- Python可迭代的物件與迭代器Python物件
- 一起探討JavaScript的物件JavaScript物件
- Golang中多大的物件算是大物件,多小的物件算是小物件?Golang物件
- (005)我們一起學Python;常用操作符Python
- (007)我們一起學Python;元組和字串Python字串
- python 關於 函式物件與閉包Python函式物件
- Python3基礎18——類與物件Python物件
- Python基礎語法(七:類與物件)Python物件
- 「譯」一起探討 JavaScript 的物件JavaScript物件
- Python實用技法第13篇:對自定義類物件排序:attrgetterPython物件排序
- Python學習之路28-符合Python風格的物件Python物件
- Python學習之變數、物件和引用Python變數物件
- (008)我們一起學Python;總結--序列類方法Python
- 和我一起理解js中的事件物件JS事件物件
- 小迪安全第12天資訊收集學習
- 卡特蘭(Catalan)數入門詳解
- 卡特蘭數和斯特林數