zoukankan      html  css  js  c++  java
  • python基础之面向对象(三))(实战:烤地瓜(SweetPotato))

    一、分析“烤地瓜”的属性和方法

    示例属性如下:

    cookedLevel : 这是数字;0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!我们的地瓜开始时是生的
    cookedString : 这是字符串;描述地瓜的生熟程度
    condiments : 这是地瓜的配料列表,比如番茄酱、芥末酱等

    示例方法如下:

    cook() : 把地瓜烤一段时间
    addCondiments() : 给地瓜添加配料
    __init__() : 设置默认的属性
    __str__() : 让print的结果看起来更好一些

    二、定义类,并且定义__init__()方法

    第一步:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author: Renyz
    # 定义一个类
    class SweetPotato:
        def __init__(self):# 初始化,描述地瓜生疏的程度
            self.cookedstring = "生的"    # 属性,表示地瓜的生熟程度,默认是生的
            self.cookedlevel = 0 # 记录地瓜生疏的程度
            #0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!
        # 烤地瓜
        def cook(self,cooked_time): #cooked_time表示时间长度
            if cooked_time >=0 and cooked_time<3:
                self.cookedstring = "地瓜生的"  # self.表示改的是对象的属性
            elif cooked_time >=3 and cooked_time<5:
                self.cookedstring = "地瓜半生不熟"
            elif cooked_time>=5 and cooked_time <8:
                self.cookedstring = "地瓜熟了"
            elif cooked_time >8:
                self.cookedstring = "地瓜烤糊了"
    # 创建一个地瓜对象
    di_gua = SweetPotato()
    # 调用方法,开始烤地瓜
    di_gua.cook(1)
    print(di_gua) #  打印的内存地址
    执行结果-->>
    <__main__.SweetPotato object at 0x000000E2665980F0>

    第二步:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author: Renyz
    # 定义一个类
    class SweetPotato:
        def __init__(self):# 初始化,描述地瓜生疏的程度
            self.cookedstring = "生的"    # 属性,表示地瓜的生熟程度,默认是生的
            self.cookedlevel = 0 # 记录地瓜生疏的程度
            #0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!
        # 烤地瓜
        def cook(self,cooked_time): #cooked_time表示时间长度
            self.cookedlevel += cooked_time
            if self.cookedlevel >=0 and self.cookedlevel<3:
                self.cookedstring  = "地瓜生的"  # self.表示改的是对象的属性
            elif self.cookedlevel >=3 and self.cookedlevel<5:
                self.cookedstring = "地瓜半生不熟"
            elif self.cookedlevel >=5 and self.cookedlevel <8:
                self.cookedstring = "地瓜熟了"
            elif self.cookedlevel >8:
                self.cookedstring = "地瓜烤糊了"
        def __str__(self): # 定义str方法  ;return 返回什么就打印什么
            return "地瓜的状态:%s(%d)"%(self.cookedstring,self.cookedlevel) 
              # 表示查看烤的程度和时间 # 创建一个地瓜对象 di_gua = SweetPotato() print(di_gua) # 调用方法,开始烤地瓜 di_gua.cook(1) print(di_gua) di_gua.cook(1) print(di_gua) di_gua.cook(3) print(di_gua)
    执行结果-->>
    地瓜的状态:生的(0) 地瓜的状态:地瓜生的(1) 地瓜的状态:地瓜生的(2) 地瓜的状态:地瓜熟了(5)

    三、给地瓜添加佐料

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author: Renyz
    # 定义一个类
    class SweetPotato:
        def __init__(self):# 初始化,描述地瓜生疏的程度
            self.cookedstring = "生的"    # 属性,表示地瓜的生熟程度,默认是生的
            self.cookedlevel = 0 # 记录地瓜生疏的程度
            #0~3表示还是生的,超过3表示半生不熟,超过5表示已经烤好了,超过8表示已经烤成木炭了!
            self.condiments = [] # 属性
        # 烤地瓜
        def cook(self,cooked_time): #cooked_time表示时间长度
            self.cookedlevel += cooked_time
            if self.cookedlevel >= 0 and self.cookedlevel < 3:
                self.cookedstring = "地瓜生的"  # self.表示改的是对象的属性
            elif self.cookedlevel >= 3 and self.cookedlevel < 5:
                self.cookedstring = "地瓜半生不熟"
            elif self.cookedlevel >= 5 and self.cookedlevel < 8:
                self.cookedstring = "地瓜熟了"
            elif self.cookedlevel > 8:
                self.cookedstring = "地瓜烤糊了"
        def zouliao(self,items):    #定义添加佐料的函数
            self.condiments.append(items)# 第一个.表示属性,第二个.表示添加
        def __str__(self): # 定义str方法  ;return 返回什么就打印什么
            return "地瓜的状态:%s(%d),添加是佐料有%s"%(self.cookedstring,self.cookedlevel,str(self.condiments))
            # 表示查看烤的程度和时间
    # 创建一个地瓜对象
    di_gua = SweetPotato()
    print(di_gua)
    # 调用方法,开始烤地瓜
    di_gua.cook(1)
    print(di_gua)
    di_gua.zouliao("槟榔")
    di_gua.cook(1)
    print(di_gua)
    di_gua.zouliao("芥末")
    di_gua.cook(1)
    di_gua.zouliao("芥末")
    print(di_gua)
    执行结果-->>
    地瓜的状态:生的(0),添加是佐料有[] 地瓜的状态:地瓜生的(1),添加是佐料有[] 地瓜的状态:地瓜生的(2),添加是佐料有['槟榔'] 地瓜的状态:地瓜半生不熟(3),添加是佐料有['槟榔', '芥末', '芥末']
  • 相关阅读:
    Windows Server 2008 R2 Enterprise 安装.NET Framework 4.0
    layer弹层content写错导致div复制了一次,导致id失效 $().val() 获取不到dispaly:none div里表单的值
    IIS 注册.NET Framework 4.0 命令
    记一次神秘又刺激的装机
    HTTP Error 503. The service is unavailable.
    找到多个与名为“Home”的控制器匹配的类型。
    Discuz论坛广告横幅大图在百度app内无法显示,百度app默认开启了广告屏蔽
    解决Antimalware Service Executable CPU,内存占用高的问题
    Discuz 部署,500 – 内部服务器错误。 您查找的资源存在问题,因而无法显示。
    IIS部署网站只有首页能访问,其他链接失效/运行.net+Access网站-可能原因:IIS未启用32位应用程序模式
  • 原文地址:https://www.cnblogs.com/renyz/p/11592180.html
Copyright © 2011-2022 走看看