與小卡特一起學python 第14章 物件

yarking207發表於2016-04-07
#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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2076518/,如需轉載,請註明出處,否則將追究法律責任。

相關文章