0.定義一個學(xué)生類匠题。有屬性:姓名、年齡、成績(語文缩功,數(shù)學(xué),英語)[每課成績的類型為整數(shù)]
方法:
a. 獲取學(xué)生的姓名:getname()
b. 獲取學(xué)生的年齡:getage()
c. 返回3門科目中最高的分數(shù)都办。get_course()
class Student(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self._score = score
for i in self._score:
if not isinstance(self._score[i], int):
raise ValueError
def getname(self):
return self.name
def getage(self):
return self.age
@property
def score(self):
return self._score
@score.setter
def score(self, value):
for i in value:
if not isinstance(value[i], int):
raise ValueError
self._age = value
def get_course(self):
max = self._score['語文']
for i in self._score:
if max < self._score[i]:
max = self._score[i]
return max
stu1 = Student('AA', 17, {'語文': 23, '數(shù)學(xué)': 88, '英語': 100})
print(stu1.score)
stu1.score = {'語文': 23, '數(shù)學(xué)': 88, '英語': 100}
print(stu1.score)
print(stu1.get_course())
1.建立一個汽車類Auto嫡锌,包括輪胎個數(shù),汽車顏色琳钉,車身重量势木,速度等成員變量,并通過不同的構(gòu)造方法創(chuàng)建實例歌懒。至少要求 汽車能夠加速 減速 停車啦桌。
再定義一個小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD等成員變量 覆蓋加速 減速的方法
class Auto(object):
def __init__(self, tyre, weight=1, speed=100):
self.tyre = tyre
self.color = '藍色'
self.weight = weight
self.speed = speed
def speedup(self):
self.speed+=1
print('加速了1')
def speeddown(self):
self.speed-=1
print('減速了1')
def stop(self):
self.speed=0
print('停車了及皂!')
class CarAuto(Auto):
def __init__(self,tyre, weight, speed,conditioner,CD):
super().__init__(tyre, weight, speed)
self.conditioner = conditioner
self.CD = CD
def speedup(self):
self.speed+=10
print('加速了10')
def speeddown(self):
self.speed-=10
print('加速了10')
C1 =CarAuto(4,2,100,'中央空調(diào)','藍蓮花')
print(C1.speeddown())
2.創(chuàng)建一個名為User 的類甫男,其中包含屬性firstname 和lastname ,還有用戶簡介通常會存儲的其他幾個屬性验烧。
在類User 中定義一個名 為describeuser() 的方法板驳,它打印用戶信息摘要;再定義一個名為greetuser() 的方法,它向用戶發(fā)出個性化的問候碍拆。
class User:
def __init__(self,firstname,synopsis,lastname=''):
self.firstname = firstname
self.lastname =lastname
self.synopsis = synopsis
def describeuser(self):
return (self.synopsis['name']+self.synopsis['age'])
def greetuser(self):
return '%s笋庄,您好!'%self.synopsis['name']
user1 = User('X',{'name':'小明','age':16},'A')
print(user1.greetuser())
3.創(chuàng)建一個Person類倔监,添加一個類字段用來統(tǒng)計Perosn類的對象的個數(shù)
class Person(object):
num = 0
def __init__(self,name='zhangsan'):
self.name = name
Person.num+=1
p1 =Person()
P2 =Person()
print(p1.name,Person.num)