![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面向对象基础
面向对象基础的语法知识
python_ID
这个作者很懒,什么都没留下…
展开
-
定义一个Person类, 类中要有初始化方法, 方法中要有人的姓名和年龄属性
# 1. 定义一个Person类,类中要有初始化方法,方法中要有人的姓名和年龄属性## 2. 将类中的姓名是公有属性,年龄是私有属性.## 3. 提供获取私有属性的公有方法 get_age方法.## 4. 提供可以设置私有属性的方法 set_age方法,要求如果输入的年龄在 0 -- 100 之间,设置年龄,否则,提示输入不正确,.## 5. 重写 __str__ 要求打印对象时,把 姓名和年龄都打印出来。class Person(object): def __init__(s原创 2020-07-25 16:03:58 · 11872 阅读 · 0 评论 -
反恐精英基础版
import randomclass Person(object): def __init__(self, name): self.life = 100 self.name = name def __str__(self): return "{}当前的生命值是:{}".format(self.name, self.life)class Hero(Person): def fire(self, p): # 伤原创 2020-07-25 16:00:52 · 173 阅读 · 0 评论 -
电脑类
# 定义一个电脑类,电脑有品牌,有价格,能放电影。# 分别创建2个对象"联想电脑" 和 "苹果电脑"。# 调用放电影的动作,联想电脑播放 电影"葫芦娃",苹果电脑播放"黑猫警长"。class Computers: """电脑类""" def __init__(self, brand, price): # 品牌 self.__brand = brand # 价格 self.__price = price def原创 2020-07-25 15:57:24 · 1227 阅读 · 0 评论 -
水果类
# 定义一个水果类,然后通过水果类,创建苹果对象、橘子对象、西瓜对象并分别添加属性:颜色和价格:class Fruits: """水果类""" def __init__(self, color, price): # 颜色属性 self.__color = color # 价格属性 self.__price = price def get_color(self): return self.__co原创 2020-07-25 15:55:53 · 6166 阅读 · 0 评论 -
手机的模拟,有耗电和充电方法, 有电量的属性
# 手机的模拟,有耗电和充电方法, 有电量的属性class Phone: """手机类""" def __init__(self): """初始化对象时调用的魔法方法""" # 默认新的手机的电量是100 self.power = 100 def music(self): """使用手机听歌""" if self.power < 5: print("当前手机电量不足以原创 2020-07-25 15:53:41 · 552 阅读 · 0 评论