タイヤの数、車の色、体重、速度および他の特性を含む、自動車の自動作成、および異なる施工方法のインスタンスを作成する1。少なくとも車両の加速及び減速停止を必要とすることができます。そして、自動継承カークラスCarAutoを定義し、エアコン、CDのプロパティを追加し、加速、減速方法をカバーするための方法を再実装
class Auto:
def __init__(self, tyre=4, color='白色', weight=2, speed=0):
self.tyre = tyre
self.color = color
self.weight = weight
self.speed = speed
def add_speed(self):
self.speed += 2
if self.speed >= 180:
self.speed = 180
def sub_speed(self):
self.speed -= 2
if self.speed < 0:
self.speed = 0
def stop(self):
self.speed = 0
class AirConditioner:
def __init__(self, breed='格力', power=1, type='冷暖'):
self.breed = breed
self.power = power
self.type = type
class CD:
def __init__(self, breed='索尼', color='黑色', price=1000):
self.breed = breed
self.color = color
self.price = price
class CarAuto(Auto):
def __init__(self, tyre=4, color='白色', weight=2, speed=0):
super().__init__(tyre, color, weight, speed)
self.air_conditioner = AirConditioner()
self.cd = CD()
def add_speed(self):
self.speed += 4
if self.speed >= 240:
self.speed = 240
def sub_speed(self):
self.speed -= 4
if self.speed <= 0:
self.speed = 0
2.オブジェクトクラス統計Perosn数のクラスのフィールドを追加し、Personクラスを作成します。
クラスパーソン:
数= 0
def __init__(self):
if self.__class__ == Person:
Person.count += 1
クラス学生(人):
パス
STU =学生()
プリント(Person.count)
P1 =人()
P2 =人()
プリント(Person.count)
`` `
3.属性を持つ、動物のクラスを作成します。フォーム:性別、年齢、色、タイプ(このクラスの印刷オブジェクトは「????性別 - 年齢 - 色 - - タイプ/ / XXXはオブジェクトのをする時間が必要印刷)
class Animal:
def __init__(self, gender='雌', color='黑色', age=2, type='爬行'):
self.gender = gender
self.color = color
self.age = age
self.type = type
def __repr__(self):
return '/{}的对象: 性别-{} 年龄-{} 颜色-{} 类型-{}/'.format(self.__class__.__name__, self.gender, self.age, self.color, self.type)
a1 = Animal()
print(a1)
4.クラスの円は半径属性、領域及び外周を有する書き込み、取得時間は、対応する値の面積と周囲長が半径の値に応じて取り得る場合に必要。しかし、割り当てのための面積と周囲長は、プログラムは直接クラッシュ、およびプロパティを変更するためのプロンプトが割り当てることはできません
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
@property
def perimeter(self):
return 2 * Circle.pi * self.radius
@perimeter.setter
def perimeter(self, value):
raise ReadOnlyError
@area.setter
def area(self, value):
raise ReadOnlyError
c1 = Circle(10)
print(c1.area, c1.perimeter)
5.書き込みライセンスとシャッフル機能(特定の属性と実際の状況に基づいて、他の機能独自の遊び)が必要とポーカーゲームクラス、
class PokerNum(Enum):
Three = (3, '3')
Four = (4, '4')
Five = (5, '5')
Six = (6, '6')
Seven = (7, '7')
Eight = (8, '8')
Nine = (9, '9')
Ten = (10, '10')
J = (11, 'J')
Q = (12, 'Q')
K = (13, 'K')
A = (14, 'A')
Two = (15, '2')
Joker_S = (16, 'Joker')
Joker_B = (17, 'JOKER')
# print(PokerNum.J, PokerNum.J.value)
# # # 获取当前枚举类中所有的数据
# for item in PokerNum.__members__.items():
# print(item, type(item[1]))
class Poker:
def __init__(self, color: str, num: PokerNum):
self.color = color # ♥、♠、♣、♦
self.num = num # 2-10,J,Q,K,A; 大王、小王
def __repr__(self):
return '{}{}'.format(self.color, self.num.value[1])
# 让Poker对象可以比较大小(>)
# p1 > p2 -> p1.__gt__(p2)
def __gt__(self, other):
return self.num.value[0] > other.num.value[0]
class PokerGame:
def __init__(self):
# 一副牌
self.pokers = []
# 创建牌
nums = PokerNum.__members__.items()
colors = ['♥', '♠', '♣', '♦']
for num in nums:
print('>>>')
print(num)
print(num[1])
if num[1] == PokerNum.Joker_S or num[1] == PokerNum.Joker_B:
continue
for color in colors:
# 创建牌对象
p = Poker(color, num[1])
self.pokers.append(p)
self.pokers.append(Poker('', PokerNum.Joker_S))
self.pokers.append(Poker('', PokerNum.Joker_B))
# print(self.pokers)
def __shuffle(self):
# 方法一: 转换成集合
# print(set(self.pokers))
# 方法二: random.shuffle(列表)
shuffle(self.pokers)
print(self.pokers)
def deal(self):
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 = PokerGame()
# game.shuffle()
print(game.deal())
print(game.deal())
1.コンテンツの決意指定されたファイルの歌詞歌詞2.時刻表示プロンプト:フィーチャーされているクラスを書くための6(TRY)歌詞ファイルは、一般的に以下の形式で保存されています。フロント歌詞に対応する時間は、歌詞が対応する時間に対応する点で表示することができています
class Lyric:
def __init__(self):
self._time = 0
self.word = ''
@property
def time(self):
return self._time
@time.setter
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
self.__lyrics = []
def __analysis_file(self):
# 1.读歌词文件中的内容
with open('files/'+self.__song_name+'.lrc', 'r', encoding='utf-8') as f: # 歌词文件要自行添加
while True:
line_content = f.readline()
if not line_content:
break
# 将时间和词分离
lines = line_content.split(']')
word = lines[-1]
for time in lines[:-1]:
lyric = Lyric()
lyric.time = time
lyric.word = word
self.__lyrics.append(lyric)
# 对歌词进行排序
self.__lyrics.sort(reverse=True)
# print(self.__lyrics)
print('解析歌词')
def get_lyric(self, time):
"""根据时间获取歌词"""
if not self.__lyrics:
self.__analysis_file()
# 找到第一个小于指定时间的歌词对象
for lyric in self.__lyrics:
if lyric.time <= time:
return lyric.word
l1 = LyricAnalysis('蓝莲花')
# l1.analysis_file()
print(l1.get_lyric(100))
print(l1.get_lyric(120))