import math
1
class Auto:
def __init__(self,wheel_nums=3,color='blue',weight=2,speed=90):
self.wheel_nums=wheel_nums
self.color=color
self.weight=weight
self.speed=speed
def speed_up(self):
self.speed+=40
def speed_down(self):
self.speed-=40
def stop(self):
self.speed=0
car1=Auto(4,'black',1500,80)
car1=Auto()
car1.stop()
print(car1.__dict__)
class CarAuto(Auto):
def __init__(self,air,carcd):
super().__init__()
self.air=air
self.carcd=carcd
def speed_up(self):
self.speed += 50
def speed_down(self):
self.speed -= 50
car2=CarAuto(1,2)
print(car2.__dict__)
2
class Person:
count_person=0
def __init__(self):
Person.count_person+=1
p1=Person()
p2=Person()
p3=Person()
print(Person.count_person)
3
class Animal:
def __init__(self,gender='雄性',age=2,color='black',type_animal='考拉'):
self.gender=gender
self.age=age
self.color=color
self.type_animal=type_animal
def __str__(self):
return '/Animal的對象:性別-%s,年齡-%d歲,顏色-%s,類型-%s/'%(
self.gender,self.age,self.color,self.type_animal)
an1=Animal()
print(an1)
4
class WriteError(Exception):
def __str__(self):
return '該屬性不能賦值!'
class Circular:
def __init__(self,radius=1):
self._radius=radius
self._girth=2*math.pi*self.radius
self._area=math.pi*(self.radius**2)
@property
def radius(self):
return self._radius
@radius.setter
def radius(self,value):
self._radius=value
self._girth = 2*math.pi*self._radius
self._area = math.pi*(self._radius**2)
@property
def girth(self):
return self._girth
@girth.setter
def girth(self,value):
raise WriteError
@property
def area(self):
return self._area
@area.setter
def area(self,value):
raise WriteError
c1=Circular()
print(c1.girth,c1.area)
c1.radius=10
print(c1.girth,c1.area)
# c1.area=100
# print(c1.girth,c1.area)__main__.WriteError: 該屬性不能賦值!
5
def trans(str1:str):#格式時間轉(zhuǎn)化為數(shù)字時間s
return int(str1[:2])*60+float(str1[3:])
with open('day15homework.txt',encoding='utf-8') as f1:
words_song = f1.readlines()
list_obj=[]
class AnalysisSong:
def __init__(self,words):
self._words=words
self._words_time=None
def show_words(self):
print(self._words)
@property
def words_time(self):
return self._words_time
@words_time.setter
def words_time(self,str1:str):
# '[00:00.20]\n'
list_str1=[]
self._words_time=trans(str1[1:9])
list_obj=[]
words_song[-1]+='\n'
for words in words_song:
str1=''
nums=''
for chr1 in words:
if '\u4e00' <= chr1 <= '\u9fff':
str1+=chr1
else:
nums+=chr1
if len(nums)>10:
while nums[10] != '\n':
nums = nums[10:]
nums1=nums[:10]
song1 = AnalysisSong(str1)
song1.words_time = nums
list_obj.append(song1)
song1=AnalysisSong(str1)
song1.words_time=nums
list_obj.append(song1)
list_obj.sort(key=lambda x:x.words_time)
input_time='01:23.33'
input_time=trans(input_time)
for i in range(len(list_obj)-1):
if list_obj[i].words_time<=input_time<=list_obj[i+1].words_time:
list_obj[i+1].show_words()#穿過幽暗地歲月