- 建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù)零院,汽車顏色,車身重量村刨,速度等屬性告抄,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車烹困。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)玄妈、CD屬性,并且重新實(shí)現(xiàn)方法覆蓋加速髓梅、減速的方法
class Auto:
bdef __init__(self, tyre, color, weight, speed):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def accelerate(self):
print('speed is up to', speed+30)
def slow_down(self):
print('speed is reduce to', speed-30)
def car_stop(self):
print('the car is stopped')
class CarAuto(Auto):
def __init__(self, air_condition, cd):
super(CarAuto, self).__init__()
self.air_condition = air_condition
self.cd = cd
def accelerate(self):
print('speed is up to', speed+20)
def slow_down(self):
print('speed is reduce to', speed-20)
- 創(chuàng)建一個(gè)Person類拟蜻,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Perosn:
count = 0
def __init__(self):
Person.count += 1
def __del__(self):
Person.count -= 1
- 創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別枯饿、年齡酝锅、顏色、類型 奢方,
class Animal:
def __init__(self, gender, age, color, species):
self._gender = gender
self._age = age
self._color = color
self._species = species
@property
def gender(self):
return Animal.__name__+'的對(duì)象:性別-'+self._gender
@property
def age(self):
return Animal.__name__+'的對(duì)象:年齡-'+str(self._age)
@property
def color(self):
return Animal.__name__+'的對(duì)象:顏色-'+self._color
@property
def species(self):
return Animal.__name__+'的對(duì)象:類型-'+self._species
要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
- 寫一個(gè)圓類搔扁, 擁有屬性半徑、面積和周長(zhǎng)蟋字;要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到稿蹲。但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰鹊奖,并且提示改屬性不能賦值
class ReadonlyError(Exception):
def __str__(self):
return '該屬性不能賦值'
class Circle:
def __init__(self):
self.redius = redius
self._area = 3.1415926*self.redius**2
self._girth = 2*3.1415926*redius
@property
def area(self):
return self._area
@area.setter
def area(self, value):
raise ReadonlyError
@property
def girth(self):
return self._girth
@girth.setter
def girth(self, value):
raise ReadonlyError
- 寫一個(gè)撲克類苛聘, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
import colorama
print(colorama.Fore.MAGENTA)
import random
class Poker:
poker_ = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'K', 'Q']
color_ = ['?', '?', '?', '?']
card = ['king', 'kinglet']
players = []
def __init__(self, n: int):
self.n = n
self.players = Poker.players
def shuffle(self):
for color in self.color_:
for poker in self.poker_:
self.card.append((color, poker))
random.shuffle(self.card)
def deal(self):
if not self.players:
for i in range(self.n):
self.players.append([])
else:
for i in range(self.n):
self.players[i].clear()
for i in range(self.n):
for j in range(3):
self.players[i].append(self.card.pop())
if len(self.card) < 3*self.n:
self.shuffle()
-
(嘗試)寫一個(gè)類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲(chǔ)的忠聚。歌詞前面對(duì)應(yīng)的是時(shí)間设哗,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞
[00:00.20]藍(lán)蓮花 [00:00.80]沒有什么能夠阻擋 [00:06.53]你對(duì)自由地向往 [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]當(dāng)你低頭地瞬間 [02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路 [02:32.17][00:42.69]心中那自由地世界 [02:37.20][00:47.58]如此的清澈高遠(yuǎn) [02:42.32][00:52.72]盛開著永不凋零 [02:47.83][00:57.47]藍(lán)蓮花
import time
class Lyrics:
def __init__(self):
self.ly1 = '藍(lán)蓮花'
self.ly2 = '沒有什么能夠阻擋'
self.ly3 = '你對(duì)自由的向往'
self.ly4 = '天馬行空的生涯'
self.ly5 = '你的心了無牽掛'
self.ly6 = '穿過幽暗的歲月'
self.ly7 = '也曾感到彷徨'
self.ly8 = '當(dāng)你低頭的瞬間'
self.ly9 = '才發(fā)現(xiàn)腳下的路'
self.ly10 = '心中那自由的世界'
self.ly11 = '如此的清澈高遠(yuǎn)'
self.ly12 = '盛開著永不凋零'
def time_shower(self, t_):
if t_ < 10:
return '[00:0%.2f]' % t_
elif t_ < 60:
return '[00:%.2f]' % t_
else:
if t_ % 60 < 10:
return '[0%d:0%.2f]' % (t_ // 60, t_ % 60)
else:
return '[0%d:%.2f]' % (t_ // 60, t_ % 60)
def show(self):
t0 = time.clock()
t1 = time.clock()
while t1 < 167.83:
t1 = time.clock()
time.sleep(0.2)
if (t1 - t0 < 0.2 or 52.72 < t1 - t0 < 57.47 or 162.32 < t1 - t0 < 167.83):
print(self.time_shower(t1), self.ly1)
if 0.2 < t1 - t0 < 0.8:
print(self.time_shower(t1), self.ly2)
if 0.8 < t1 - t0 < 6.53:
print(self.time_shower(t1), self.ly3)
if 6.53 < t1 - t0 < 11.59:
print(self.time_shower(t1), self.ly4)
if 11.59 < t1 - t0 < 16.53:
print(self.time_shower(t1), self.ly5)
if (16.53 < t1 - t0 < 21.95 or 57.47 < t1 - t0 < 110.22 or 125.72 < t1 - t0 < 131.27):
print(self.time_shower(t1), self.ly6)
if (21.95 < t1 - t0 < 26.83 or 110.22 < t1 - t0 < 115.46 or 131.27 < t1 - t0 < 136.51):
print(self.time_shower(t1), self.ly7)
if (26.83 < t1 - t0 < 32.30 or 115.46 < t1 - t0 < 120.60 or 136.51 < t1 - t0 < 141.81):
print(self.time_shower(t1), self.ly8)
if (32.30 < t1 - t0 < 37.16 or 120.60 < t1 - t0 < 125.72 or 141.81 < t1 - t0 < 146.79):
print(self.time_shower(t1), self.ly9)
if (37.16 < t1 - t0 < 42.69 or 146.79 < t1 - t0 < 152.17):
print(self.time_shower(t1), self.ly10)
if (42.69 < t1 - t0 < 47.58 or 152.17 < t1 - t0 < 157.20):
print(self.time_shower(t1), self.ly11)
if (47.58 < t1 - t0 < 52.72 or 157.20 < t1 - t0 < 162.32):
print(self.time_shower(t1), self.ly12)