-
建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto: def __init__(self, num_tires, color, height, speed): self.num_tires = num_tires self.color = color self.height = height self.speed = speed def speed_up(self, speed_num): new_speed = self.speed + speed_num print('加速,速度从%d提升到%d' % (self.speed, new_speed)) self.speed = new_speed def speed_down(self, speed_num): new_speed = self.speed - speed_num print('加速,速度从%d降到%d' % (self.speed, new_speed)) self.speed = new_speed def park(self): self.speed = 0 print('停车') class CarAuto(Auto): def __init__(self, air_conditioner, cd, num_tires, color, height, speed): super().__init__(num_tires, color, height, speed) self.air_conditioner = air_conditioner self.cd = cd def speed_up(self, speed_num): new_speed = self.speed + speed_num * 2 print('加速,速度从%d提升到%d' % (self.speed, new_speed)) self.speed = new_speed def speed_down(self, speed_num): new_speed = self.speed - speed_num * 2 print('加速,速度从%d降到%d' % (self.speed, new_speed)) self.speed = new_speed car = CarAuto('美的', '清明上河图', 4, '红', 4000, 60) car.speed_up(20) # 加速,速度从60提升到100 car.speed_down(40) # 加速,速度从100降到20 car.park() # 停车
-
创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person: num = 0 def __init__(self): if self.__class__ == Person: Person.num += 1 class Student(Person): pass p1 = Person() p2 = Person() s = Student() print(Person.num) # 2
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal: def __init__(self, gender, age, color, kind): self.gender = gender self.age = age self.color = color self.kind = kind def __repr__(self): return '/{}的对象:性别-{} 年龄-{} 颜色-{} 类型-{}/'.format(self.__class__.__name__,self.gender, self.age, self.color, self.kind) a = Animal('雄', 2, '黄色', '狗') print(a)
-
写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class ReadOnly(Exception): def __str__(self): return '该属性不能赋值' class Circle: pi = 3.14 def __init__(self, r): self.r = r self._area = 0 self._perimeter = 0 @property def area(self): return Circle.pi * self.r * self.r @area.setter def area(self, value): raise ReadOnly @property def perimeter(self): return 2 * Circle.pi * self.r @perimeter.setter def perimeter(self, value): raise ReadOnly c = Circle(2) c.r = 4 print('圆的面积:', c.area) # 圆的面积: 50.24 print('圆的周长:', c.perimeter) # 25.12 c.area = 12 # __main__.ReadOnly: 该属性不能赋值r
-
写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import enum import random # 5.写一个扑克游戏类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥) # 如果一个类继承Enum这个类,那么这个类就是枚举类 class PokerNum(enum.Enum): J = (11, 'J') Q = (12, 'Q') K = (13, 'K') A = (14, 'A') Two = (15, '2') Three = (3, '3') Four = (4, '4') Five = (5, '5') Sex = (6, '6') Seven = (7, '7') Eight = (8, '8') Nine = (9, '9') Ten = (10, '10') Joker_s = (16, 'joker') Joker_b = (17, 'JOKER') # 获取枚举中的所有数据 # print(PokerNum.Joker_s, PokerNum.Joker_s.value) # for item in PokerNum.__members__.items(): # print(item[1]) class Poker: def __init__(self, color, num): self.color = color # ♥,♠,♣,♦ self.num = num # 2-10,J,Q,K,A,大王,小王 def __repr__(self): return '{}{}'.format(self.color, self.num.value[1]) # 让Poker对象支持比较大小 p1.__gt__(p2) def __gt__(self, other): return self.num.value[0] > other.num.value[0] class PokerGame: def __init__(self): self.pokers = [] nums = PokerNum.__members__.items() colors = ['♥', '♠', '♣', '♦'] for num in nums: if num[1] == PokerNum.Joker_s or num[1] == PokerNum.Joker_b: continue for color in colors: # 创建牌对象 p = Poker(color, num[1]) self.pokers.append(p) self.pokers.append(Poker('', PokerNum.Joker_s)) self.pokers.append(Poker('', PokerNum.Joker_b)) def __shuffle(self): # 方法1: # set(self.pokers) # 方法2:random.shuffle(列表) random.shuffle(self.pokers) def deal(self): self.__shuffle() poker_iter = iter(self.pokers) p1 = [] p2 = [] p3 = [] for _ in range(17): p1.append(next(poker_iter)) p2.append(next(poker_iter)) p3.append(next(poker_iter)) # p1.sort(key=lambda item: item.num.value[0], reverse=True) # p2.sort(key=lambda item: item.num.value[0], reverse=True) # p3.sort(key=lambda item: item.num.value[0], reverse=True) p1.sort() p2.sort() p3.sort() return p1, p2, p3, list(poker_iter) game = PokerGame() print(game.deal()) # 运算符重载 # 所有类型都是类,每个运算符都对应一个固定的魔法方法,使用运算符其实是在调用对应的魔法方法 p1 = Poker('♥', PokerNum.J) p2 = Poker('♥', PokerNum.K) print(p1 < p2)
-
(尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
[00:00.20]蓝莲花 [00:00.80]没有什么能够阻挡 [00:06.53]你对自由地向往 [00:11.59]天马行空的生涯 [00:16.53]你的心了无牵挂 [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月 [02:16.51][01:55.46][00:26.83]也曾感到彷徨 [02:21.81][02:00.60][00:32.30]当你低头地瞬间 [02:26.79][02:05.72][00:37.16]才发觉脚下的路 [02:32.17][00:42.69]心中那自由地世界 [02:37.20][00:47.58]如此的清澈高远 [02:42.32][00:52.72]盛开着永不凋零 [02:47.83][00:57.47]蓝莲花
import enum import random # 5.写一个扑克游戏类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥) # 如果一个类继承Enum这个类,那么这个类就是枚举类 class PokerNum(enum.Enum): J = (11, 'J') Q = (12, 'Q') K = (13, 'K') A = (14, 'A') Two = (15, '2') Three = (3, '3') Four = (4, '4') Five = (5, '5') Sex = (6, '6') Seven = (7, '7') Eight = (8, '8') Nine = (9, '9') Ten = (10, '10') Joker_s = (16, 'joker') Joker_b = (17, 'JOKER') # 获取枚举中的所有数据 # print(PokerNum.Joker_s, PokerNum.Joker_s.value) # for item in PokerNum.__members__.items(): # print(item[1]) class Poker: def __init__(self, color, num): self.color = color # ♥,♠,♣,♦ self.num = num # 2-10,J,Q,K,A,大王,小王 def __repr__(self): return '{}{}'.format(self.color, self.num.value[1]) # 让Poker对象支持比较大小 p1.__gt__(p2) def __gt__(self, other): return self.num.value[0] > other.num.value[0] class PokerGame: def __init__(self): self.pokers = [] nums = PokerNum.__members__.items() colors = ['♥', '♠', '♣', '♦'] for num in nums: if num[1] == PokerNum.Joker_s or num[1] == PokerNum.Joker_b: continue for color in colors: # 创建牌对象 p = Poker(color, num[1]) self.pokers.append(p) self.pokers.append(Poker('', PokerNum.Joker_s)) self.pokers.append(Poker('', PokerNum.Joker_b)) def __shuffle(self): # 方法1: # set(self.pokers) # 方法2:random.shuffle(列表) random.shuffle(self.pokers) def deal(self): self.__shuffle() poker_iter = iter(self.pokers) p1 = [] p2 = [] p3 = [] for _ in range(17): p1.append(next(poker_iter)) p2.append(next(poker_iter)) p3.append(next(poker_iter)) # p1.sort(key=lambda item: item.num.value[0], reverse=True) # p2.sort(key=lambda item: item.num.value[0], reverse=True) # p3.sort(key=lambda item: item.num.value[0], reverse=True) p1.sort() p2.sort() p3.sort() return p1, p2, p3, list(poker_iter) game = PokerGame() print(game.deal()) # 运算符重载 # 所有类型都是类,每个运算符都对应一个固定的魔法方法,使用运算符其实是在调用对应的魔法方法 p1 = Poker('♥', PokerNum.J) p2 = Poker('♥', PokerNum.K) print(p1 < p2)
作业-day16-面向对象
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...