python的类的实际联系--烤地瓜和搬家具
#coding:utf-8
2 class SweetPotato():
3 def __init__(self):
4 #先初始化对象
5 self.cook_time = 0
6 self.cook_state = "生的"
7 self.condiments = []#调味料
8
9 def cook(self,time):
10 """烤地瓜方法"""
11 self.cook_time += time
12 if 0 <= self.cook_time < 3:
13 self.cook_state = "生的"
14 if 3 <= self.cook_time < 5:
15 self.cook_state = "半生不熟"
16 if 5 <= self.cook_time < 8:
17
18 self.cook_state = "熟的"
19 if self.cook_time >= 8:
20 self.cook_state = "烤糊了"
21 def add_condiments(self,condiment):
22
23 #用户根据自己的爱好添加配料
24 self.condiments.append(condiment)
def __str__(self):
27 return f"时间:{self.cook_time},状态:{self.cook_sta te},配料{self.condiments}"
28
29 digui1 = SweetPotato()
30 print(digui1)
31 digui1.cook(2)
32 digui1.add_condiments("酱油")
33 print(digui1)
#运行结果
时间:0,状态:生的,配料[]
时间:2,状态:生的,配料['酱油']
#conding:utf-8
2 """"
3 定义类:
4 1.家具类
5 2.房子类
6 再通过类来创建对象
7
8 """""
9 class fitment():
10 def __init__(self,fitment_name,area):
11 self.fitment_name = fitment_name
12 self.area = area
13
14 class house():
15 def __init__(self,location,area):
16 #初始化实例的基本属性
17 self.location = location
18 self.area = area
19 self.remainint_area = area
20 self.fitments = []#空列表
21 def add_fitments(self,fitment):
22 if fitment.area > self.remainint_area:
23 print("该家具放不下")
24 return
25 else:
26 self.fitments.append(fitment.fitment_name)
27 self.remainint_area -= fitment.area
28 print("家具摆放成功!")
29 return
30 def __str__(self):
31 return f"房屋的位置:{self.location},房子的面积:{s elf.area},剩余面积{self.remainint_area},房屋家具有{self.fit ments}"
32
33 bed = fitment("双人床",6)
34 sofa = fitment("沙发",10)
35 home = house("北京",1000)
36 home.add_fitments(bed)
#运行结果
家具摆放成功!
房屋的位置:北京,房子的面积:1000,剩余面积994,房屋家具有['双人床']
从代码中我们可以体会到面对对象编程的好处,可以是代码的变得十分简洁。
python的类的实际联系--烤地瓜和搬家具的更多相关文章
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 【python】类中的self
在python的类中,经常会写self,代表对象自己.如下例: #coding=utf-8 class Foo: def __init__(self, name): self.name = name ...
- python基础——类和实例
python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...
- python(6)-类
面向对象编程是一种编程方式,此编程方式的落地需要使用 "类" 和 "对象" 来实现,所以,面向对象编程其实就是对 "类" 和 "对 ...
- 从C#到Python —— 4 类及面向对象
http://www.cnblogs.com/yanxy/archive/2010/04/04/c2p_4.html 如果你熟悉C#,那么对类(Class)和面向对象(Object Oriented) ...
- Python type类具体的三大分类:metaclasses,classes,instance
Python type类视角中的对象体系需要我们不断的学习,其中我们使用的时候需要注意.下面我们就看看如何才能更好的运用Python type类.下面的文章希望大家有所收获. 在单纯的Python t ...
- python元类分析
刚開始接触到Python新式类中的元类的概念的时候非常是纠结了下..不知道这是个啥东西... 用下面几个定义来说明吧: (1)Python中,类也是对象..仅仅只是这样的对象比較的特殊,他用于创建别的 ...
- Python用类实现串以及对串的方法进行单元测试
串是一种常见的数据结构,这里使用Python定义类来实现相应的方法.先看代码,再对相关知识进行讲解. # coding=utf-8 __all__=['ADTString'] class ADTStr ...
- Python基础-类
Python基础-类 @(Python)[python, python基础] 写在前面 如非特别说明,下文均基于Python3 摘要 本文重点讲述如何创建和使用Python类,绑定方法与非绑定方法的区 ...
随机推荐
- codeforces 10C Digital Root(非原创)
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...
- μC/OS-III---I笔记4---软件定时器
软件定时器是在硬件定时器的基础上开发的,通过将一个硬件定时器进行分频及管理就可以的到多个软件定时器.他和时间管理共同组成了系统的时间管理大部分的内容.系统一开始的系统初始化函数OSInit函数内调用了 ...
- Tailwind CSS in Action
Tailwind CSS in Action Tailwind CSS是一个高度可定制的低级CSS框架,它为您提供了构建定制设计所需的所有构造块,而无需烦恼要覆盖的烦人的自以为是的样式 https:/ ...
- React Native & Security
React Native & Security https://reactnative.dev/docs/security React Native blogs https://reactna ...
- what's the difference amount of pdf, epub, and mobi format
what's the difference amount of pdf, epub, and Mobi format What is the difference between pdf, epub ...
- node.js module.exports & exports & module.export all in one
node.js module.exports & exports & module.export all in one cjs const log = console.log; log ...
- Versatile Python 3.x
Versatile Python 3.x TryPython Python 3.8.0 (default, Nov 14 2019, 22:29:45) [GCC 5.4.0 20160609] on ...
- iOS effect & swiper delete components
iOS effect & swiper delete components mint-ui & cell-swipe https://elemefe.github.io/mint-ui ...
- free Google translator for the personal website
free Google translator for the personal website https://html5.xgqfrms.xyz/
- django学习-23.admin管理后台的数据表数据的自定义展示
目录结构 1.前言 2.自定义设置一张指定的数据表的列表展示内容 2.1.第一步:如果我们想让数据表[hello_person]里面的表字段值全部展示出来,需在应用[hello]里的[admin.py ...