- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法
class Auto:
def __init__(self):
self.color = "red"
self.weight = "100KG"
self.speed = "60km/h"
self.num_tire = 4
def speed_down(self, time):
self.speed = str(int(self.speed[0:-4])- int(time) * 15) + "km/h"
if int(self.speed[0:-4]) >= 0:
return self.speed
else:
return "0km/h"
def speed_up(self, time):
self.speed = str(int(self.speed[0:-4]) + int(time) * 15 )+ "km/h"
if int(self.speed[0:-4]) <= 140:
return self.speed
else:
return "140km/h"
@staticmethod
def shut_down():
print("此时速度为0")
class CarAuto(Auto):
def __init__(self):
super().__init__()
self.air_conditioning = "开空调"
self.music = "Play CD"
def speed_down(self, time):
self.speed = str(int(self.speed[0:-4])- int(time) * 10) + "km/h"
if int(self.speed[0:-4]) >= 0:
return self.speed
else:
return "0km/h"
def speed_up(self, time):
self.speed = str(int(self.speed[0:-4]) + int(time) * 10 )+ "km/h"
if int(self.speed[0:-4]) <= 160:
return self.speed
else:
return "160km/h"
auto1 = Auto()
auto1.shut_down()
print(auto1.speed_down(3))
print(auto1.speed_up(8))
print(auto1.__dict__)
carauto1 = CarAuto()
carauto1.shut_down()
print(carauto1.speed_down(3))
print(carauto1.speed_up(8))
print(carauto1.__dict__)
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
numbers = 0
def __init__(self, name):
self.name = name
Person.numbers += 1
@classmethod
def number(cls):
return cls.numbers
p1 = Person('张三')
p2 = Person('李四')
p3 = Person('陈武')
print(Person.numbers,Person.number()) # 3 3
-
创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
def __init__(self,brand,gender,age,color):
self.brand = brand
self.gender = gender
self.age = age
self.color = color
def __repr__(self):
return "/" + Animal.__name__ + "的对象:" +str(self.__dict__)[1:-1].replace(":","-") + "/"
print(Animal("中华田园犬", "公", 2, "white"))
#/Animal的对象:'brand'- '中华田园犬', 'gender'- '公', 'age'- 2, 'color'- 'white'/
- 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
import math
class ReadOnlyError(Exception):
def __str__(self):
return '改属性不能赋值'
class Circle:
def __init__(self, radius):
self.radius = radius
self._area = None
self._perimeter = None
@property
def area(self):
return math.pi * self.radius ** 2
@area.setter
def area(self,value):
raise ReadOnlyError
@property
def perimeter(self):
return 2 * math.pi * self.radius
@perimeter.setter
def perimeter(self, value):
raise ReadOnlyError
rectangle1 = Circle(10)
print(rectangle1.area,rectangle1.perimeter)
rectangle1.radius = 20
print(rectangle1.area,rectangle1.perimeter)
rectangle1.perimeter = 300
-
(尝试)写一个类,其功能是: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 LyricsAnalysis:
"""歌词解析"""
def __init__(self):
self.lyrics = {}
def analysis(self):
with open("lyrics.txt",encoding="utf-8") as f:
content = f.read().splitlines()
for item in content:
lyric = item.split("]")
for index in range(len(lyric) - 1):
self.lyrics[lyric[index][1::]] = lyric[-1]
print(list(self.lyrics))
def display(self,time):
list_time = list(self.lyrics)
list_time.sort()
for index in range(len(list_time)):
if time < "00:00.20" :
print("蓝莲花")
elif time >= "02:47.83":
print("播放完毕")
else:
if list_time[index] <= time < list_time[index + 1]:
print(self.lyrics[list_time[index + 1]])
#for time1 in list_time:
#print(self.lyrics[time1])
lanlianhua = LyricsAnalysis()
lanlianhua.analysis()
lanlianhua.display("02:30.28 ")