建立一個汽車類Auto,包括輪胎個數(shù),汽車顏色,車身重量,速度等屬性,
并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車。
再定義一個小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD屬性,
并且重新實(shí)現(xiàn)方法覆蓋加速、減速的方法
class Auto:
"""汽車類:Auto,
屬性:汽車顏色,重量,速度,輪胎個數(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)建一個Person類,添加一個類字段用來統(tǒng)計(jì)Perosn類的對象的個數(shù)
class Person:
count = 0
def __init__(self):
Person.count += 1
創(chuàng)建一個動物類,擁有屬性:性別、年齡、顏色、類型 ,
要求打印這個類的對象的時候以'/XXX的對象:
性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印
class Animal:
"""類:動物
屬性:性別、年齡、顏色、類型"""
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__)+'的對象:性別-'+self.sex+', 年齡-'+str(self.age)+', 顏色-'+str(self.color)+', 類型-'+str(self.type1)
animal = Animal('母', 2, 'black', 'dog')
print(animal)
寫一個圓類, 擁有屬性半徑、面積和周長;
要求獲取面積和周長的時候的時候可以根據(jù)半徑的值把對應(yīng)的值取到。
但是給面積和周長賦值的時候,程序直接崩潰,并且提示改屬性不能賦值
class WriteError(Exception):
def __str__(self):
return '該屬性不能賦值!'
class Circle:
"""類:圓
屬性:半徑,面積,周長"""
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
寫一個撲克類:
要求擁有發(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): # 三個人每人發(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.(嘗試)寫一個類,其功能是:
1.解析指定的歌詞文件的內(nèi)容
2.按時間顯示歌詞
提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲的。
歌詞前面對應(yīng)的是時間,在對應(yīng)的時間點(diǎn)可以顯示對應(yīng)的歌詞
class Lyrics:
"""類:歌詞"""
pass
class Parsing:
"""
類:解析器
"""
pass