question1.建立一個汽車類Auto,包括輪胎個數(shù)虱黄,汽車顏色悦即,車身重量,速度等屬性橱乱,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例辜梳。至少要求 汽車能夠加速 減速 停車。
再定義一個小汽車類CarAuto 繼承Auto 并添加空調(diào)泳叠、CD屬性作瞄,并且重新實(shí)現(xiàn)方法覆蓋加速、減速的方法
class Auto:
def __init__(self, make, model, weight, color='白色', tyre=4):
self.make = make.capitalize()
self.model = model.capitalize()
self.weight = weight
self.tyre = tyre
self.color = color
self.speed = 0
def show_car(self):
print('這是一輛%s %s,顏色:%s,車重:%dkg,輪胎:%d個' % (self.make, self.model, self.color, self.weight, self.tyre))
def update_speed(self, value):
if self.speed == 0:
self.speed = value
print('初始化車速成功')
self.show_speed()
else:
print('錯誤:只能對速度為0的車初始化速度')
def increment_speed(self, value):
self.speed += value
print('加速成功')
self.show_speed()
def decrement_speed(self, value):
if value > self.speed:
print('錯誤:減少速度大于汽車當(dāng)前速度')
elif value == self.speed:
self.stop()
else:
self.speed -= value
print('減速成功')
self.show_speed()
def stop(self):
self.speed = 0
print('停車成功')
print('%s %s已停車' % (self.make, self.model))
def show_speed(self):
print('%s %s當(dāng)前車速為%dkm/h' % (self.make, self.model, self.speed))
car1 = Auto('audi', 'a4', 1560)
car2 = Auto('BMW', '320i', 1570, '黑色')
car3 = Auto('benz', 'glc260', 1860, '藍(lán)色', 4)
car1.show_car()
car2.show_car()
car3.show_car()
car1.show_speed()
car1.update_speed(20)
car1.increment_speed(40)
car1.decrement_speed(30)
car1.stop()
"""
這是一輛Audi A4,顏色:白色,車重:1560kg,輪胎:4個
這是一輛Bmw 320i,顏色:黑色,車重:1570kg,輪胎:4個
這是一輛Benz Glc260,顏色:藍(lán)色,車重:1860kg,輪胎:4個
Audi A4當(dāng)前車速為0km/h
初始化車速成功
Audi A4當(dāng)前車速為20km/h
加速成功
Audi A4當(dāng)前車速為60km/h
減速成功
Audi A4當(dāng)前車速為30km/h
停車成功
Audi A4已停車
"""
class CarAuto(Auto):
def __init__(self, make, model, weight, color='白色', tyre=4, air_conditioner=0, cd=0):
super().__init__(make, model, weight, color, tyre)
self._air_conditioner = air_conditioner
self._cd = cd
@property
def air_conditioner(self):
if self._air_conditioner:
print('這輛車有空調(diào)危纫,可以吹空調(diào)')
return self._air_conditioner
else:
print('這輛車沒空調(diào)宗挥,好蠢')
return self.air_conditioner
@property
def cd(self):
if self._cd:
print('這輛車有CD,可以聽音樂')
return self._cd
else:
print('這輛車沒CD种蝶,好蠢')
return self._cd
def show_car(self):
super().show_car()
self.air_conditioner
self.cd
def useless(self):
if self._air_conditioner and self._cd:
print('吹著空調(diào)聽著音樂好爽')
elif self._air_conditioner:
print('只吹空調(diào)一般爽')
elif self._cd:
print('只聽音樂一般般')
else:
print('又沒空調(diào)又沒音樂賊難受')
def increment_speed(self, value):
super().increment_speed(value)
self.useless()
def decrement_speed(self, value):
super().decrement_speed(value)
self.useless()
car4 = CarAuto('tesla', 'model s', 1660, '銀色', 4, 1)
car4.show_car()
car4.update_speed(30)
car4.increment_speed(50)
car4.decrement_speed(30)
car4.stop()
"""
這是一輛Tesla Model s,顏色:銀色,車重:1660kg,輪胎:4個
這輛車有空調(diào)契耿,可以吹空調(diào)
這輛車沒CD,好蠢
初始化車速成功
Tesla Model s當(dāng)前車速為30km/h
加速成功
Tesla Model s當(dāng)前車速為80km/h
只吹空調(diào)一般爽
減速成功
Tesla Model s當(dāng)前車速為50km/h
只吹空調(diào)一般爽
停車成功
Tesla Model s已停車
"""
question2.創(chuàng)建一個Person類蛤吓,添加一個類字段用來統(tǒng)計(jì)Person類的對象的個數(shù)
class Person:
x = (x for x in range(1, 10000))
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Person.count = next(Person.x)
p1 = Person('張三', 20)
print('%s類當(dāng)前共創(chuàng)建%d個對象' % (Person.__name__, Person.count))
p2 = Person('李四', 30)
print('%s類當(dāng)前共創(chuàng)建%d個對象' % (Person.__name__, Person.count))
p3 = Person('王五', 40)
print('%s類當(dāng)前共創(chuàng)建%d個對象' % (Person.__name__, Person.count))
p4 = Person('趙一', 40)
print('%s類當(dāng)前共創(chuàng)建%d個對象' % (Person.__name__, Person.count))
"""
Person類當(dāng)前共創(chuàng)建1個對象
Person類當(dāng)前共創(chuàng)建2個對象
Person類當(dāng)前共創(chuàng)建3個對象
Person類當(dāng)前共創(chuàng)建4個對象
"""
question3.創(chuàng)建一個動物類宵喂,擁有屬性:性別、年齡会傲、顏色锅棕、類型 拙泽,
要求打印這個類的對象的時候以'/XXX的對象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
class Animals:
def __init__(self, gender, age, color, species):
self.gender = gender
self.age = age
self.color = color
self.species = species
def __repr__(self):
return '%s的對象:性別-%s 年齡-%d歲 顏色-%s 類型-%s' % (Animals.__name__,
self.gender, self.age, self.color, self.species)
pet1 = Animals('公', 5, '白色', '泰迪犬')
print(pet1)
"""
Animals的對象:性別-公 年齡-5歲 顏色-白色 類型-泰迪犬
"""
question4.寫一個圓類, 擁有屬性半徑裸燎、面積和周長顾瞻;要求獲取面積和周長的時候的時候可以根據(jù)半徑的值把對應(yīng)的值取到。
但是給面積和周長賦值的時候德绿,程序直接崩潰荷荤,并且提示改屬性不能賦值
class WriteError(Exception):
def __str__(self):
return '不能修改只讀的屬性'
class Circle:
pi = 3.1415926
def __init__(self, radius=0):
self._radius = radius
self._perimeter = 0
self._area = 0
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
self._radius = value
self._perimeter = 2 * Circle.pi * self._radius
self._area = Circle.pi * self._radius ** 2
@property
def perimeter(self):
return self._perimeter
@perimeter.setter
def perimeter(self, value):
raise WriteError
@property
def area(self):
return self._area
@area.setter
def area(self, value):
raise WriteError
r1 = Circle()
r1.radius = 5
print(r1.perimeter, r1.area)
# r1.perimeter = 20
"""
31.415926 78.539815
Traceback (most recent call last):
raise WriteError
__main__.WriteError: 不能修改只讀屬性
"""
question5.(嘗試)寫一個類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲的移稳。
歌詞前面對應(yīng)的是時間蕴纳,在對應(yīng)的時間點(diǎn)可以顯示對應(yīng)的歌詞
import time
"""
class FileOperation:
def __init__(self, filename):
self.filename = filename
def read_txt(self):
temp_list = []
with open(self.filename) as file_obj:
for line in file_obj:
temp_list.append(line.rstrip())
return temp_list
"""
class LyricManage:
def __init__(self, filename):
self.filename = filename
self._lyric = None
self._dict_lyric = None
@property
def lyric(self):
temp_list = []
with open(self.filename) as file_obj:
for line in file_obj:
temp_list.append(line.rstrip())
self._lyric = temp_list
return self._lyric
@property
def dict_lyric(self):
temp_dict = {}
for lis in self.lyric:
lis_1 = lis.split(']')
for i in range(len(lis_1)-1):
key = float(lis_1[i][1:3]) * 60 + float(lis_1[i][4:9])
value = lis_1[-1]
temp_dict[key] = value
self._dict_lyric = dict(sorted(temp_dict.items()))
return self._dict_lyric
@dict_lyric.setter
def dict_lyric(self, value):
raise ValueError
def play_lyric(self):
start_time = time.clock()
for key in self.dict_lyric:
while True:
end_time = time.clock()
if round(key, 2) == round(end_time, 2):
print(self.dict_lyric[key])
break
lyric1 = LyricManage('lyric/lanlianhua.txt')
lyric1.play_lyric()