1.建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù)床牧,汽車顏色荣回,車身重量,速度等屬性戈咳,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例心软。至少要求 汽車能夠加速 減速 停車壕吹。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD屬性删铃,并且重新實(shí)現(xiàn)方法覆蓋加速耳贬、減速的方法
class Auto:
def __init__(self, tires, color, weight):
self._tires = tires
self._color = color
self._weight = weight
self.speed = 0
@property
def color(self):
return self.color
@color.setter
def color(self, value):
self._color = value
@property
def tires(self):
return self.tires
@tires.setter
def tires(self, value):
raise ValueError
@property
def weight(self):
return self.weight
@weight.setter
def weight(self, value):
raise ValueError
def park(self):
self.speed = 0
print('汽車變?yōu)橥V範(fàn)顟B(tài)!')
def speed_up(self):
if self.speed + 20 > 200:
print('已經(jīng)是最高速度泳姐!')
return
self.speed += 20
print('速度加至%.1f邁' %self.speed)
def slow_down(self):
if self.speed - 20 <= 0:
print('速度減至停止?fàn)顟B(tài)效拭!')
self.speed = 0
else:
self.speed -= 20
print('速度減為%.1f邁' %self.speed)
def info(self):
print('汽車輪胎數(shù)為%d,顏色為%s,重量為%.1f噸,現(xiàn)速度為%.1f邁'%(self._tires,self._color,self._weight,self.speed))
auto_car = Auto(4, '黑色', 1.3)
auto_car.info()
test1 = type(auto_car)(6, '黃色', 2.3)
test1.info()
test2 = auto_car.__class__(4, '灰色',1.6)
test2.info()
class CarAuto(Auto):
def __init__(self, tires, color, weight,air_condition = 'off', cd = 'off'):
super().__init__(tires, color, weight)
self._air_condition = air_condition
self._cd = cd
self.speed = 0
@property
def air_conditon(self):
if self._air_condition == 'on':
return '空調(diào)打開暂吉!'
elif self._air_condition == 'off':
return '空調(diào)關(guān)閉胖秒!'
@air_conditon.setter
def air_condition(self, value):
self._air_condition = value
@property
def cd(self):
if self._cd == 'on':
return 'cd打開!'
else:
return 'cd關(guān)閉慕的!'
@cd.setter
def cd(self, value):
self._cd = value
def speed_up(self):
if self.speed + 10 > 100:
print('已經(jīng)是最高速度阎肝!')
return
self.speed += 10
print('速度加至%.1f邁' % self.speed)
def slow_down(self):
if self.speed - 10 <= 0:
print('速度減至停止?fàn)顟B(tài)!')
self.speed = 0
else:
self.speed -= 10
print('速度減為%.1f邁' % self.speed)
def info(self):
print('汽車輪胎數(shù)為%d,顏色為%s,重量為%.1f噸,現(xiàn)速度為%.1f邁,空調(diào)處于%s狀態(tài)肮街,cd處于%s狀態(tài)'\
% (self._tires, self._color, self._weight, self.speed, self._air_condition, self._cd))
car = CarAuto(4, '黃色', 1.2, 'on', 'on')
car.info()
2.創(chuàng)建一個(gè)Person類风题,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person:
num = 0
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
Person.num += 1
Person.count()
def eat(self, food):
print('%s在吃%s'%(self.name, food))
@classmethod
def count(cls):
print('現(xiàn)在已經(jīng)創(chuàng)建了%d個(gè)對(duì)象!'%Person.num)
a = Person('小明',12,'男')
b = Person('小花',13,'女')
3.創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別嫉父、年齡沛硅、顏色、類型 绕辖,
要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
class Animal:
def __init__(self, gender, age, color, type):
self._gender = gender
self.age = age
self.color = color
self._type = type
@property
def gender(self):
return self._gender
@gender.setter
def gender(self, value):
raise ValueError
@property
def type(self):
return self.type
@type.setter
def type(self, value):
raise ValueError
def print_info(self):
print('/%s的對(duì)象:性別-%s 年齡-%d 顏色-%s 類型-%s'%(self.__class__.__name__,self._gender,\
self.age,self.color,self._type))
animal = Animal('雄性',4,'黑色','哺乳類')
animal.print_info()
4.寫一個(gè)圓類摇肌, 擁有屬性半徑、面積和周長(zhǎng)仪际;要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到围小。
但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰树碱,并且提示改屬性不能賦值
class WriteError(Exception):
def __str__(self):
return '屬性不能修改值'
class Circle:
pi = 3.1415926
def __init__(self, radius):
self.radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
return Circle.pi * (self.radius ** 2)
@area.setter
def area(self, value):
raise WriteError
@property
def perimeter(self):
return 2 * Circle.pi * self.radius
@perimeter.setter
def perimeter(self, value):
raise WriteError
circle = Circle(3)
print('%.2f,%.2f'%(circle.area,circle.perimeter))
circle.area = 10
print(circle.area)
5.寫一個(gè)撲克類肯适, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
import random
import operator
class Poker:
def __init__(self, color, count):
self.color = color
self.count = count
class Pokers:
color_list = ['紅桃','黑桃','方塊','梅花']
count_list = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
def __init__(self):
self.poker_list = []
self.framer1 = []
self.framer2 = []
self.landowner = []
self.hole_cards = []
self.landowner_num = 0
self.framer1_num = 0
self.framer2_num = 0
for color in Pokers.color_list:
for count in Pokers.count_list:
self.poker_list.append(Poker(color, count))
self.poker_list.append(Poker('王','大'))
self.poker_list.append(Poker('王','小'))
def poker_shuffle(self):
random.shuffle(self.poker_list)
def sort_poker(self, sort_list):
cmp_list = ['3','4','5','6','7','8','9','10','J','Q','K','A','2','小','大']
for i in cmp_list:
for j in sort_list[:]:
if j.count == i:
temp = j
sort_list.remove(j)
sort_list.append(temp)
def deal(self):
Pokers.poker_shuffle(self)
pokers_iter = iter(self.poker_list)
i = 1
for _ in range(51):
if i == 1:
self.landowner.append(next(pokers_iter))
self.landowner_num += 1
elif i == 2:
self.framer1.append(next(pokers_iter))
self.framer1_num += 1
elif i == 3:
self.framer2.append(next(pokers_iter))
self.framer2_num += 1
i = 0
i += 1
self.hole_cards = list(pokers_iter)
self.landowner.extend(self.hole_cards[:])
self.landowner_num += 3
def show(self):
print('\n==============底牌==============')
self.sort_poker(self.hole_cards)
for poker in self.hole_cards:
print(poker.__dict__)
print('\n==============地主牌==============')
self.sort_poker(self.landowner)
for poker in self.landowner:
print(poker.__dict__)
print('當(dāng)前牌數(shù):%d'%self.landowner_num)
print('\n==============農(nóng)民一牌==============')
self.sort_poker(self.framer1)
for poker in self.framer1:
print(poker.__dict__)
print('當(dāng)前牌數(shù):%d'%self.framer1_num)
print('\n==============農(nóng)民二牌==============')
self.sort_poker(self.framer2)
for poker in self.framer2:
print(poker.__dict__)
print('當(dāng)前牌數(shù):%d'%self.framer2_num)
a = Pokers()
a.deal()
a.show()
6.寫一個(gè)類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式
進(jìn)行存儲(chǔ)的成榜。歌詞前面對(duì)應(yīng)的是時(shí)間框舔,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
import operator
import time
class Lyric:
def __init__(self, time, lrc):
self.time = time
self.lrc = lrc
class Song:
def __init__(self):
self.song = []
self.time1 = 0
def load_lyric(self):
with open('files\lyric','r',encoding='utf-8')as f:
for line in f:
time = []
splite1 = line.strip().split(']')
for _ in splite1:
if _.startswith('['):
time.append(_.replace('[',''))
else:
lyc = _
for _ in time:
self.song.append(Lyric(_, lyc))
def sort_lyc(self):
cmp = operator.attrgetter('time')
self.song.sort(key = cmp)
def output(self):
timestamp1 = time.time()
time_tuple = time.localtime(57599)
while True:
s = time.asctime(time_tuple)
print(s[11:19],end='')
time.sleep(1)
print('\r', end='', flush=True)
timestamp = time.time()
time_tuple = time.localtime(timestamp - timestamp1 + 57599)
for i in range(len(self.song)):
tf = time.strftime("%M:%S", time_tuple)
if tf == self.song[i].time[:5]:
if self.song[i].lrc != '':
print(self.song[i].lrc)
def start(self):
self.load_lyric()
self.sort_lyc()
self.output()
s = Song()
s.start()
#files\lyric
[00:02.00]周杰倫 - 稻香
[00:04.00]詞:周杰倫 曲:周杰倫
[00:06.00]
[00:08.00]www.90lrc.cn 歡迎您的光臨!
[00:10.00]
[00:30.86]對(duì)這個(gè)世界如
[00:32.50]果你有太多的抱怨
[00:34.38]跌倒了
[00:35.48]就不敢繼續(xù)往前走
[00:37.30]為什么
[00:38.45]人要這么的脆弱 墮落
[00:41.31]請(qǐng)你打開電視看看
[00:43.16]多少人 為生命在努力
[00:45.45]勇敢的走下去
[00:47.17]我們是不是該知足
[00:49.53]珍惜一切 就算沒有擁有
[00:53.32]
[00:54.02]還記得你說家是唯一的城堡
[00:57.69]隨著稻香河流繼續(xù)奔跑
[01:00.63]微微笑 小時(shí)候的夢(mèng)我知道
[01:05.81]不要哭讓螢火蟲帶著你逃跑
[01:09.42]鄉(xiāng)間的歌謠永遠(yuǎn)的依靠
[01:12.34]回家吧 回到最初的美好
[01:17.65]
[01:41.02]不要這么容易就想放棄
[01:43.35]就像我說的
[01:44.77]追不到的夢(mèng)想
[01:45.75]換個(gè)夢(mèng)不就得了
[01:47.86]為自己的人生鮮艷上色
[01:49.92]先把愛涂上喜歡的顏色
[01:51.98]
[01:52.68]笑一個(gè)吧
[01:53.81]功成名就不是目的
[01:55.72]讓自己快樂快樂
[01:57.01]這才叫做意義
[01:58.62]童年的紙飛機(jī)
[02:00.12]現(xiàn)在終于飛回我手里
[02:03.67]
[02:04.37]所謂的那快樂
[02:05.71]赤腳在田里追蜻蜓追到累了
[02:08.62]偷摘水果被蜜蜂給叮到怕了
[02:11.75]誰在偷笑呢
[02:13.29]我靠著稻草人 吹著風(fēng)
[02:15.27]唱著歌 睡著了
[02:16.81]哦 哦~
[02:17.57]午后吉它在蟲鳴中更清脆
[02:19.65]哦 哦~
[02:20.50]陽光灑在路上就不怕心碎
[02:23.00]珍惜一切 就算沒有擁有
[02:26.96]
[02:27.66]還記得你說家是唯一的城堡
[02:31.36]隨著稻香河流繼續(xù)奔跑
[02:34.25]微微笑 小時(shí)候的夢(mèng)我知道
[02:39.40]不要哭讓螢火蟲帶著你逃跑
[02:43.06]鄉(xiāng)間的歌謠永遠(yuǎn)的依靠
[02:46.08]回家吧 回到最初的美好
[02:50.38]
[02:51.08]還記得你說家是唯一的城堡
[02:54.73]隨著稻香河流繼續(xù)奔跑
[02:57.73]微微笑 小時(shí)候的夢(mèng)我知道
[03:02.76]不要哭讓螢火蟲帶著你逃跑
[03:06.50]鄉(xiāng)間的歌謠永遠(yuǎn)的依靠
[03:09.46]回家吧 回到最初的美好
[03:15.31]~~End~~