1. 建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù)移国,汽車顏色,車身重量道伟,速度等屬性迹缀,并通過(guò)不同的構(gòu)造方法創(chuàng)建實(shí)例。
至少要求 汽車能夠加速 減速 停車蜜徽。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)祝懂、CD屬性,并且重新實(shí)現(xiàn)方法覆蓋加速拘鞋、減速的方法
import math
class Auto:
wheels = 4
def __init__(self, color, weight, speed=0):
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self):
for i in range(5):
print("正在加速砚蓬,當(dāng)前時(shí)速%dkm/s" % (self.speed + int(i)))
def speed_down(self):
for i in range(5):
print("正在減速,當(dāng)前時(shí)速%dkm/s" % (self.speed - int(i)))
@staticmethod
def parking():
print('停車~~~~~~~~~~')
class CarAuto(Auto):
def __init__(self, color, weight, speed, air_condition, cd_player):
super().__init__(color, weight, speed)
self.air_condition = air_condition
self.cd_player = cd_player
def speed_up(self):
for i in range(5):
print("正在加速掐禁,當(dāng)前時(shí)速%dkm/s" % (self.speed + 3*int(i)))
def speed_down(self):
for i in range(5):
print("正在減速怜械,當(dāng)前時(shí)速%dkm/s" % (self.speed - 3*int(i)))
car1 = CarAuto('黑色', '3T', 90, '空調(diào)', 'BOSE')
print(car1.__dict__)
car1.speed_up()
print('==============================================')
car1.speed_down()
car1.parking()
2. 創(chuàng)建一個(gè)Person類,添加一個(gè)類字段用來(lái)統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)
class Person:
num = 0
@classmethod
def count(cls):
cls.num += 1
return cls.num
p1 = Person()
print('建立的對(duì)象個(gè)數(shù)%d個(gè)' % p1.count())
p2 = Person()
print('建立的對(duì)象個(gè)數(shù)%d個(gè)' % p2.count())
3. 創(chuàng)建一個(gè)動(dòng)物類傅事,擁有屬性:性別缕允、年齡、顏色蹭越、類型 障本,要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來(lái)打印
class Animals:
def __init__(self, gender, age, color, type):
self.gender = gender
self.age = age
self.color = color
self.type = type
def print_animals(self):
print('性別-%s 年齡-%d 顏色-%s 類型-%s' % (self.gender, self.age, self.color, self.type))
animals1 = Animals('女', 3, 'black', '豹子')
animals1.print_animals()
4. 寫(xiě)一個(gè)圓類, 擁有屬性半徑响鹃、面積和周長(zhǎng)驾霜;要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值
把對(duì)應(yīng)的值取到。但是給面積和周長(zhǎng)賦值的時(shí)候买置,程序直接崩潰粪糙,并且提示改屬性不能賦值
class ReadError(Exception):
def __str__(self):
print('該數(shù)據(jù)不能修改!7尴睢H馗浴3俏琛!寞酿!')
class Circle:
def __init__(self, radius):
self.radius = radius
self._area = 0
self._perimeter = 0
@property
def area(self):
return math.pi*self.radius**2
@property
def perimeter(self):
return 2*math.pi*self.radius
@area.setter
def area(self, value):
raise ReadError
@perimeter.setter
def perimeter(self, value):
raise ReadError
c1 = Circle(6)
print(c1.area)
print(c1.perimeter)
c1.area = 1000
# print(c1.area) # 該數(shù)據(jù)不能修改<叶帷!7サ@觥!惨好!