Python类的使用——烤地瓜例子(一个类)

#写代码一定要从最基础的需求一步一步完善,切忌想一口吃成大胖子
#利用类的面向对象编程优势之一:实现了函数之间的变量互通,更好的体现了“封装”的特点
class SweetPotato:
    def __init__(self):
        self.cookedLevel=0
        self.cookedString="生的"
        self.contiments=[]
    def __str__(self):
        x="地瓜目前是{},包含的佐料有{}".format(self.cookedString,self.contiments)
        return x
    def cook(self,times):
        self.cookedLevel+=times
        if self.cookedLevel>8:
            self.cookedString="焦的"
        elif self.cookedLevel>5:
            self.cookedString = "熟的"
        elif self.cookedLevel>3:
            self.cookedString="半生不熟的"
        else:
            self.cookedString = "生的"
    def addcondiments(self,condiment):
        self.contiments.append(condiment)
        
digua=SweetPotato()
print(digua)
digua.cook(1)
print(digua)
digua.addcondiments("芥末酱")
print(digua)
#写类的步骤:
# 1.定义类名
# 2.创建对象,考虑初始化参数
# 3.根据初始化参数定义__init__函数(构造函数),不仅包含创建对象时的输入参数,还有一些基本属性
# 4.打印对象信息
# 5.根据对象打印要求定义__str__函数
# 6.根据对象功能丰富类的方法和属性
#   6.1 先写出功能及参数
#   6.2 根据功能需要定义方法及参数
#   6.3 判断是否需要修改__init__函数或__str__函数
#   6.4 在功能实现的基础上完善细节

# 1.定义类名
class SweetPotato:
    # 3.根据初始化参数定义__init__函数(构造函数),不仅包含创建对象时的输入参数,还有一些基本属性
    def __init__(self):
        self.cookedLevel=0
        self.cookedString="生的"
        #6.3判断是否需要修改__init__函数或__str__函数
        self.condiments=[]
    # 5.根据对象打印要求定义__str__函数
    def __str__(self):
        x="地瓜目前是{}".format(self.cookedString)
        # 6.3判断是否需要修改__init__函数或__str__函数
        x+="需要添加的佐料有:{}".format(self.condiments)
        return x
    # 6.根据对象功能丰富类的方法和属性
    # 6.2 根据功能需要定义方法及参数
    def cook(self,times):
        self.cookedLevel+=times
        if self.cookedLevel>8:
            self.cookedString="焦的"
        elif self.cookedLevel>5:
            self.cookedString="熟的"
        elif self.cookedLevel>3:
            self.cookedString="半生不熟的"
        else:
            self.cookedString="生的"
    # 6.2 根据功能需要定义方法及参数
    def addcondiments(self,condiment):
        self.condiments.append(condiment)
# 2.创建对象,考虑初始化参数
digua=SweetPotato()
# 4.打印对象信息
print(digua)
# 6.1 先写出功能及参数
digua.cook(1)
# 6.1 先写出功能及参数
digua.addcondiments("芥末酱")
print(digua)

# 6.4 在功能实现的基础上完善细节
class SweetPotato:
    def __init__(self):
        self.cookedLevel=0
        self.cookedString="生的"
        self.condiments=[]
    def __str__(self):
        x="地瓜目前是{}".format(self.cookedString)
        if len(self.condiments)!=0:
            x += "需要添加的佐料有:"
            x+=",".join(self.condiments)
        else:
            x+="没有添加任何佐料"
        return x
    def cook(self,times):
        self.cookedLevel+=times
        if self.cookedLevel>8:
            self.cookedString="焦的"
        elif self.cookedLevel>5:
            self.cookedString="熟的"
        elif self.cookedLevel>3:
            self.cookedString="半生不熟的"
        else:
            self.cookedString="生的"
    def addcondiments(self,condiment):
        self.condiments.append(condiment)

digua=SweetPotato()
print(digua)
digua.cook(1)
digua.addcondiments("芥末酱")
print(digua)
digua.addcondiments("番茄酱")
print(digua)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值