-
建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto: def __init__(self, number, colour, weight): if 2 < isinstance(number, int): self.number = number self.colour = colour self.weight = weight self._speed = 0 @property def speed(self): return self._speed @speed.setter def speed(self, valeu): if valeu < self._speed: print('减速') elif valeu > self._speed: print('加速') elif valeu == self._speed: print('速度不变') elif valeu == 0: print('停车') self._speed = valeu class CarAuto(Auto): def __init__(self, number, colour, weight, air_condition, cd): super().__init__(number, colour, weight) self.air_condition = air_condition self.cd = cd @property def speed(self): return self._speed @speed.setter def speed(self, valeu): if valeu < self._speed: print('小汽车减速') elif valeu > self._speed: print('小汽车加速') elif valeu == self._speed: print('小汽车速度不变') elif valeu == 0: print('小汽车停车') self._speed = valeu p1 = CarAuto(4, '黄色', 500, '空调', '最炫名族风') print(p1.speed) p1.speed = 60 print(p1.speed)
-
创建一个Person类,添加一个类字段用来统计Person类的对象的个数
class Person: num = 6100000000 nums = 0 def __init__(self, name, sex='男', skin_color='黄皮肤', language='chinese', state='cn', age=0): self.name = name self.age = age self.sex = sex self.skin_color = skin_color self.language = language self.state = state Person.nums += 1 @classmethod def number(cls): print(cls.nums) p1 = Person('xiaoming') p2 = Person('xiaohong') p3 = Person('xiaoxiao') p4 = Person('xiaoxia') Person.number()
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self, sex, age, colour, genre):
self.sex = sex
self.age = age
self.colour = colour
self.genre = genre
def show(self):
return '/Animal的对象:性别-%s 年龄-%s 颜色-%s 类型-%s/' % (self.sex, self.age, self.colour, self.genre)
a1 = Animal('雄性', 2, '黑色', '哈士奇')
print(a1.show())
-
写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi class FzError(Exception): def __str__(self): return '改属性不能赋值' class Roundness: def __init__(self, radius): if isinstance(radius, int) or isinstance(radius, float): if radius > 0: self.radius = radius self._perimeter = 0 self._area = 0 @property def perimeter(self): print('半径是:', self.radius) return '周长为:%s' % (2*pi*self.radius) @perimeter.setter def perimeter(self, vale): raise FzError @property def area(self): print('半径是:', self.radius) return '面积为:%s' % (pi * self.radius ** 2) @area.setter def area(self, vale): raise FzError r1 = Roundness(6) print(r1.area) print(r1.perimeter) # r1.area = 1 # __main__.FzError: 改属性不能赋值
-
写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random class Poker: def __init__(self): self.pokers = [] self.nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] self.colors = ['♥', '♠', '♣', '♦'] def poker(self): for num in self.nums: for color in self.colors: self.pokers.append('%s%s' % (color, num)) self.pokers.extend(['小王', '大王']) # print(self.pokers) def riffle(self): random.shuffle(self.pokers) def deal(self, n): a = [] for x in self.pokers[0:n]: a.append(x) self.pokers.remove(x) print(a) p1 = Poker() p1.poker() # print(p1.pokers) p1.riffle() print(p1.pokers) p1.deal(17) # 发n张牌 p1.deal(17) p1.deal(17) print(p1.pokers) # 剩下的牌
(尝试)写一个类,其功能是: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]蓝莲花
class Time_song:
def __init__(self, time=0):
self._time1 = time
def show(self):
with open('lyric', 'r', encoding='utf-8') as f:
lyric1 = f.read()
lyric2 = ''
for i in lyric1[:]:
if '\u4e00' <= i <= '\u9fa5' or i == '\u000A':
lyric2 += i
print(lyric2)
t1 = Time_song()
t1.show()