多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 实操案例:烤土豆 为了更好的理解面向对象编程,下面以“烤土豆”为案例,进行分析 :-: ![](https://img.kancloud.cn/2a/7d/2a7d69884c8a37a18e760bdb3bfc7d4c_490x367.png) ## 1.分析“烤土豆”的属性和方法 ### 示例属性如下: * cookedLevel : 这是数字;0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!我们的土豆开始时时生的 * cookedString : 这是字符串;描述土豆的生熟程度 * condiments : 这是土豆的配料列表,比如番茄酱、芥末酱等 ### 示例方法如下: * `cook()`: 把土豆烤一段时间 * `addCondiments()`: 给土豆添加配料 * `__init__()`: 设置默认的属性 * `__str__()`: 让print的结果看起来更好一些 ## 2\. 定义类,并且定义`__init__()`方法 ~~~ # 定义土豆类 class  Potato: """这是土豆的类""" #定义初始化方法 def __init__(self): self.cookedLevel = 0 self.cookedString = "生的" self.condiments = [] ~~~ ## 3\. 添加"烤土豆"方法 ~~~ #烤地瓜方法 def cook(self, time): self.cookedLevel += time if self.cookedLevel > 8: self.cookedString = "烤成灰了" elif self.cookedLevel > 5: self.cookedString = "烤好了" elif self.cookedLevel > 3: self.cookedString = "半生不熟" else: self.cookedString = "生的" ~~~ ## 4\. 基本的功能已经有了一部分,赶紧测试一下 把上面2块代码合并为一个程序后,在代码的下面添加以下代码进行测试 ~~~ myPotato = Potato() print(myPotato.cookedLevel) print(myPotato.cookedString) print(myPotato.condiments) ~~~ 完整的代码为: ~~~ class Potato: """这是烤地瓜的类""" # 定义初始化方法 def __init__(self): self.cookedLevel = 0 self.cookedString = "生的" self.condiments = [] # 烤土豆方法 def cook(self, time): self.cookedLevel += time if self.cookedLevel > 8: self.cookedString = "烤成灰了" elif self.cookedLevel > 5: self.cookedString = "烤好了" elif self.cookedLevel > 3: self.cookedString = "半生不熟" else: self.cookedString = "生的" # 用来进行测试 myPotato = Potato() print(myPotato .cookedLevel) print(myPotato .cookedString) print(myPotato .condiments) ~~~ :-: ![](https://img.kancloud.cn/c5/c3/c5c3b5b3b0ddc70bc147d647661d0e57_470x102.png) ## 5\. 测试cook方法是否好用 在上面的代码最后面添加如下代码: ~~~ print("------接下来要进行烤地瓜了-----") myPotato.cook(4) #烤4分钟 print(myPotato.cookedLevel) print(myPotato.cookedString) ~~~ :-: ![](https://img.kancloud.cn/33/87/3387383ca844fbc5b589e2804ccae56a_341x158.png) ## 6\. 定义`addCondiments()`方法和`__str__()`方法 ~~~ def __str__(self): msg = self.cookedString + " 土豆" if len(self.condiments) > 0: msg = msg + "(" for temp in self.condiments: msg = msg + temp + ", " msg = msg.strip(", ") msg = msg + ")" return msg def addCondiments(self, condiments): self.condiments.append(condiments) ~~~ ## 7\. 再次测试 完整的代码如下: ~~~ class Potato: """这是烤土豆的类""" # 定义初始化方法 def __init__(self): self.cookedLevel = 0 self.cookedString = "生的" self.condiments = [] # 定制print时的显示内容 def __str__(self): msg = self.cookedString + " 土豆" if len(self.condiments) > 0: msg = msg + "(" for temp in self.condiments: msg = msg + temp + ", " msg = msg.strip(", ") msg = msg + ")" return msg # 烤地瓜方法 def cook(self, time): self.cookedLevel += time if self.cookedLevel > 8: self.cookedString = "烤成灰了" elif self.cookedLevel > 5: self.cookedString = "烤好了" elif self.cookedLevel > 3: self.cookedString = "半生不熟" else: self.cookedString = "生的" # 添加配料 def addCondiments(self, condiments): self.condiments.append(condiments) # 用来测试的代码 myPotato = Potato() print("------有了一个土豆,还没有烤-----") print(myPotato.cookedLevel) print(myPotato.cookedString) print(myPotato.condiments) print("------接下来要进行烤土豆了-----") print("------土豆经烤了4分钟-----") myPotato.cook(4) #烤4分钟 print(myPotato) print("------土豆又经烤了3分钟-----") myPotato.cook(3) #又烤了3分钟 print(myPotato) print("------接下来要添加配料-番茄酱------") myPotato.addCondiments("番茄酱") print(myPotato) print("------土豆又经烤了5分钟-----") myPotato.cook(5) #又烤了5分钟 print(myPotato) print("------接下来要添加配料-芥末酱------") myPotato.addCondiments("芥末酱") print(myPotato) ~~~ :-: ![](https://img.kancloud.cn/c3/98/c398fd7a47e22a7154f90361eacae107_327x397.png)