建立一個(gè)汽車類Auto匾效,包括輪胎個(gè)數(shù)舷蟀,汽車顏色,車身重量面哼,速度等屬性野宜,
并通過(guò)不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車魔策。
再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)匈子、CD屬性,
并且重新實(shí)現(xiàn)方法覆蓋加速闯袒、減速的方法
class Auto:
"""汽車類:Auto虎敦,
屬性:汽車顏色,重量政敢,速度其徙,輪胎個(gè)數(shù)"""
def __init__(self, color, weight, speed, tires_num):
self.color = color
self.weight = weight
self.speed = speed
self.tires_num = tires_num
def speed_up(self, value):
self.speed += 1
def speed_down(self, value):
self.speed -= 1
def parking(self, value):
self.speed = 0
class CarAuto(Auto):
"""小汽車類,繼承Auto
屬性添加:空調(diào)堕仔,cd屬性"""
def __init__(self, color, weight, speed, tires_num, air_con, cd):
super().__init__(color, weight, speed, tires_num)
self.air_con = air_con
self.cd = cd
def speed_up(self, value):
self.speed += 2
print('當(dāng)前速度加速到%s' % self.speed)
def speed_down(self, value):
self.speed -= 2
print('當(dāng)前速度減速到%s' % self.speed)
創(chuàng)建一個(gè)Person類擂橘,添加一個(gè)類字段用來(lái)統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person:
count = 0
def __init__(self):
Person.count += 1
創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別摩骨、年齡通贞、顏色、類型 恼五,
要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象:
性別-? 年齡-? 顏色-? 類型-?/' 的形式來(lái)打印
class Animal:
"""類:動(dòng)物
屬性:性別昌罩、年齡、顏色灾馒、類型"""
def __init__(self, sex='公', age=0, color='yellow', type1='cat'):
self.sex = sex
self.age = age
self.color = color
self.type1 = type1
def __str__(self):
return str(self.__class__)+'的對(duì)象:性別-'+self.sex+'茎用, 年齡-'+str(self.age)+', 顏色-'+str(self.color)+'睬罗, 類型-'+str(self.type1)
animal = Animal('母', 2, 'black', 'dog')
print(animal)
寫(xiě)一個(gè)圓類轨功, 擁有屬性半徑、面積和周長(zhǎng)容达;
要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到古涧。
但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰花盐,并且提示改屬性不能賦值
class WriteError(Exception):
def __str__(self):
return '該屬性不能賦值!'
class Circle:
"""類:圓
屬性:半徑羡滑,面積菇爪,周長(zhǎng)"""
pi = 3.1415926
def __init__(self, radius):
self.r = radius
self._area = 0
self._per = 0
@property
def area(self):
return Circle.pi * self.r ** 2
@area.setter
def area(self, value):
raise WriteError
@property
def per(self):
return 2 * Circle.pi * self.r
@per.setter
def per(self, value):
raise WriteError
c1 = Circle(2)
print(c1.per)
c1.radius = 3
print(c1.per)
import random
寫(xiě)一個(gè)撲克類:
要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)
class Poke:
poke = [] # 撲克牌牌堆
p1 = [] # 玩家一牌堆
p2 = [] # 玩家二牌堆
p3 = [] # 玩家三牌堆
last = None # 底牌牌堆
def __init__(self,f,num): # 初始化牌堆
self.flower = f # 花色
self.num = num # 點(diǎn)數(shù)
def __str__(self):
return "%s%s" % (self.flower,self.num) # 返回牌值
@classmethod
def init(cls): # 定義牌堆
ph = ("?","?","?","?") # 花色元組
pnum = ("2","3","4","5","6","7","8","9","10","J","Q","K","A") # 點(diǎn)數(shù)元組
king = {"big":"大王","small":"小王"} # 大小王
for p in ph: # 循環(huán)遍歷花色
for _nump in pnum: # 循環(huán)遍歷點(diǎn)數(shù)
cls.poke.append(Poke(p,_nump)) # 裝牌
cls.poke.append(Poke(king["big"],"")) # 裝大王
cls.poke.append(Poke(king["small"],"")) # 裝小王
@classmethod
def wash(cls): # 洗牌
random.shuffle(cls.poke)
@classmethod
def send(cls): # 發(fā)牌
for _ in range(0,17): # 三個(gè)人每人發(fā)17張牌 循環(huán)
cls.p1.append(cls.poke.pop(0)) # 玩家一發(fā)牌
cls.p2.append(cls.poke.pop(0))
cls.p3.append(cls.poke.pop(0))
cls.last= tuple(cls.poke) # 最后三張牌做底牌 不能修改做元組
@classmethod
def show(cls): # 展示牌
print("gamer1:")
for pokes in cls.p1:
print(pokes,end = " ")
print()
print("gamer2:")
for pokes in cls.p2:
print(pokes, end=" ")
print()
print("gamer3:")
for pokes in cls.p3:
print(pokes, end=" ")
print()
print("ending:")
for pokes in cls.last:
print(pokes, end=" ")
print()
Poke.init()
Poke.wash()
Poke.send() # 必須先發(fā)牌后才能展示牌
Poke.show()
6.(嘗試)寫(xiě)一個(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 Lyrics:
"""類:歌詞"""
pass
class Parsing:
"""
類:解析器
"""
pass