1. 建立一個(gè)汽車類Auto亚斋,包括輪胎個(gè)數(shù),汽車顏色拂募,車身重量庭猩,速度等屬性,并通過(guò)不同的構(gòu)造方法創(chuàng)建實(shí)例陈症。至少要求 汽車能夠加速 減速 停車蔼水。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD屬性录肯,并且重新實(shí)現(xiàn)方法覆蓋加速趴腋、減速的方法
class Auto:
def __init__(self, tyre=4, color='白色', weight=2, speed=0):
self.tyre = tyre
self.color = color
self.weight = weight
self.seppd = speed
def add_speed(self):
self.seppd += 2
if self.seppd >= 180:
self.seppd = 180
def sub_seppd(self):
self.seppd -= 2
if self.seppd == 0:
self.seppd = 0
def stop(self):
self.seppd = 0
class AirConditioner:
def __init__(self, breed='空調(diào)', power=1, type='冷暖'):
self.breed = breed
self.power = power
self.type = type
class CD:
def __init__(self, breed='索尼', color='黑色', price=10000):
self.breed = breed
self.color = color
self.price = price
class CarAuto(Auto):
def __init__(self, tyre=4, color='白色', weight=2, speed=0):
super(CarAuto, self).__init__(tyre, color, weight, speed)
self.air_conditioner = AirConditioner()
self.CD = CD()
def add_speed(self):
self.seppd += 4
if self.seppd >= 240:
self.seppd = 240
def sub_seppd(self):
self.seppd -= 4
if self.seppd == 0:
self.seppd = 0
2. 創(chuàng)建一個(gè)Person類,添加一個(gè)類字段用來(lái)統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person:
count = 0
def __init__(self):
if self.__class__ == Person: # 判斷類型
Person.count += 1
class Student(Person):
pass
stu = Student()
print(Person.count)
cls = Person()
print(Person.count)
3. 創(chuàng)建一個(gè)動(dòng)物類论咏,擁有屬性:性別优炬、年齡、顏色厅贪、類型 蠢护,
?要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來(lái)打印
class Animals:
def __init__(self, type='陸上跑的', color='黑色', age=0, gender='母'):
self.type = type
self.color = color
self.age = age
self.gender = gender
def __repr__(self):
# self.__class__.__name_ 打印對(duì)象
return '/{}的對(duì)象: 性別-{} 年齡-{} 顏色-{} 類型-{}/'.format(self.__class__.__name__, self.gender, self.age, self.color, self.type)
a1 = Animals()
print(a1)
4. 寫一個(gè)圓類, 擁有屬性半徑养涮、面積和周長(zhǎng)葵硕;要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到眉抬。但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰懈凹,并且提示改屬性不能賦值
class ReadOnlyError(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 * self.radius
@area.setter
def area(self, value):
raise ReadOnlyError
@property
def perimeter(self):
return 2 * Circle.pi * self.radius
@perimeter.setter
def perimeter(self, value):
raise ReadOnlyError
c1 = Circle(10)
print(c1.area, c1.perimeter)
# c1.area = 100
5. 寫一個(gè)撲克類吐辙, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
# 導(dǎo)入枚舉模塊
import enum
from random import shuffle
class PokerNum(enum.Enum):
"""
創(chuàng)建枚舉類
"""
J = (11, 'J')
Q = (12, 'Q')
K = (13, 'K')
A = (14, 'A')
Two = (15, '2')
Three = (3, '3')
Four = (4, '4')
Five = (5, '5')
Six = (6, '6')
Seven = (7, '7')
Eight = (8, '8')
Nine = (9, '9')
Ten = (10, '10')
Joker_S = (16, 'joker')
Joker_D = (17, 'JOKER')
# print(PokerNum.J, PokerNum.K.value)
# print(PokerNum.__members__)
# for item in PokerNum.__members__.items():
# print(item, type(item[1]))
class Poker:
"""
撲克類
創(chuàng)建對(duì)象:顏色和張數(shù)
"""
def __init__(self, color, num):
self.color = color # ? ? ? ?
self.num = num # 2-10,J,Q,K,A;小王蘸劈,大王
def __repr__(self):
return '{}{}'.format(self.color, self.num.value[1])
# 讓Poker對(duì)象可以比較大小(>):重載魔法方法
def __gt__(self, other):
return self.num.value[0] > other.num.value[0]
class PockerGame:
"""
玩游戲類
"""
def __init__(self):
self.pokers = []
# 創(chuàng)建牌
nums = PokerNum.__members__.items()
colors = ['?', '?', '?', '?']
for num in nums:
if num[1] == PokerNum.Joker_S or num[1] == PokerNum.Joker_D:
continue
for color in colors:
# if num[1] != PokerNum.Joker_S and num[1] != PokerNum.Joker_D:
# 創(chuàng)建牌對(duì)象
p = Poker(color, num[1])
self.pokers.append(p)
self.pokers.append(Poker('', PokerNum.Joker_S))
self.pokers.append(Poker('', PokerNum.Joker_D))
print(self.pokers)
def __shuffle(self):
"""
定義為私有的方法
洗牌
方法1:轉(zhuǎn)換為集合
方法2:random.shuffle(列表)
:return:
"""
# print(set(self.pokers))
shuffle(self.pokers)
print(self.pokers)
def deal(self):
"""
在發(fā)牌之前先調(diào)用洗牌
:return:
"""
self.__shuffle()
poker_iter = iter(self.pokers)
p1 = []
p2 = []
p3 = []
for _ in range(17):
p1.append(next(poker_iter))
p2.append(next(poker_iter))
p3.append(next(poker_iter))
# 排序
# p1.sort(key=lambda item: item.num.value[0], reverse=True)
# p2.sort(key=lambda item: item.num.value[0], reverse=True)
# p3.sort(key=lambda item: item.num.value[0], reverse=True)
p1.sort(reverse=True)
p2.sort(reverse=True)
p3.sort(reverse=True)
return p1,p2,p3,list(poker_iter)
game = PockerGame()
# game.shuffle()
print(game.deal())
# p1 = Poker('?', PokerNum.J)
# p2 = Poker('?', PokerNum.K)
# print(p1<p2)
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)的歌詞
class Lyric:
def __init__(self):
self._time = 0 # 時(shí)間
self.word = '' # 歌詞
@property
def time(self):
return self._time
@time.setter # 把時(shí)間轉(zhuǎn)為秒的形式
def time(self, value):
fen = float(value[1:3])
miao = float(value[4:])
self._time = fen*60 + miao
def __repr__(self):
"""自定義打印"""
return '{}:{}'.format(self.time, self.word)
def __lt__(self, other):
return self._time > other._time
class LyricAnalysis:
def __init__(self, song_name: str):
self.__song_name = song_name # 歌名(對(duì)象屬性)
self.__lyrics = [] # 用于存儲(chǔ)歌詞與時(shí)間
def __analysis_file(self):
# 讀歌詞文件中的內(nèi)容
with open('file/'+self.__song_name + '.lrc', 'r', encoding='utf-8') as r_f:
while True:
line_content = r_f.readline()
if not line_content:
break
# 將時(shí)間和歌詞分離
lines = line_content.split(']')
# 時(shí)間點(diǎn)和歌詞產(chǎn)生一一對(duì)應(yīng)關(guān)系
# print(lines)
# word = lines[-1][:-1] # 讓歌詞一行顯示
word = lines[-1]
for time in lines[:-1]:
lyric = Lyric()
lyric.time = time
lyric.word = word
self.__lyrics.append(lyric)
# 對(duì)歌詞進(jìn)行排序(先重寫比較大小的方法)
self.__lyrics.sort()
# print(self.__lyrics)
def get_lyric(self, time):
"""根據(jù)時(shí)間獲取歌詞"""
if not self.__lyrics: # 在實(shí)現(xiàn)功能之前先解析歌詞文件(判斷是否已經(jīng)解析過(guò))
self.__analysis_file()
# 找到第一個(gè)小于指定時(shí)間的歌詞對(duì)象
for lyric in self.__lyrics:
if lyric.time <= time:
return lyric.word
l1 = LyricAnalysis('藍(lán)蓮花')
print(l1.get_lyric(10))