作业1
"""
1.学生类。属性:姓名、年龄、成绩(语文、数学、英语)
方法:a.获取学生名字, b.获取学生年龄, c.返回3门成绩的最高分
"""
class Student:
def __init__(self, name='', age=0, score=[]):
self.name = name
self.age = age
self.score = score
def getname(self):
return self.name
def getage(self):
return self.age
def get_course(self):
max_score = self.score[0]
for item in self.score:
if max_score['score'] < item['score']:
max_score = item
return max_score
if __name__ == '__main__':
score = [{'name': 'math', 'score': 90}, {'name': 'english', 'score': 84}, {'name': 'language', 'score': 87}]
stu = Student('xxx', 12, score)
print(stu.getname())
print(stu.getage())
print(stu.get_course())
作业2
"""
2.汽车类Auto。属性:轮胎个数,汽车颜色,车身重量,速度,通过不同的构造方法创建实例
方法:a.加速, b.减速, c.停车
小汽车类CarAuto继承Auto,添加属性:空调、CD; 覆盖加速 减速方法
"""
class Auto:
def __init__(self, tires=4, color='', weight=600, speed=0):
self.tires = tires
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, speed=3):
self.speed += speed
def speed_down(self, speed=3):
if self.speed <= speed:
self.speed = 0
else:
self.speed -= speed
def stop(self):
self.speed = 0
class CarAuto(Auto):
def __init__(self, air_conditioner='', cd=''):
super().__init__()
self.air_conditioner = air_conditioner
self.CD = cd
def speed_up(self, speed=3):
self.speed += speed
print('小汽车在加速')
def speed_down(self, speed=3):
if self.speed <= speed:
self.speed = 0
else:
self.speed -= speed
print('小汽车在减速')
if __name__ == '__main__':
auto1 = Auto(5, 'red')
auto2 = Auto(5, 'gray', speed=45)
auto2.speed_up(10)
auto2.speed_down(20)
auto2.stop()
car = CarAuto()
car.speed_up(10)
car.speed_down(20)
car.stop()
作业3
"""
3.User类。属性:firstname,lastname,用户简介(存用户其他属性)
方法:a.describeuser():打印用户信息, b.greetuser()向用户发出个性化问候
Admin类:属性:继承user类。privileges=[]; 方法:show_privileges()显示用户权限
要求:创建Admin实例,并调用show_privileges()这个方法
"""
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.abstract = []
def describeuser(self):
print('firstname:%s, lastname:%s' % (self.firstname, self.lastname), end=',')
print(self.abstract)
def greetuser(self):
print('你好,...欢迎您!')
class Admin(User):
def __init__(self, firstname, lastname, privileges=[]):
super().__init__(firstname, lastname)
self.privileges = privileges
def show_privileges(self):
print('%s %s的权限有:%s' % (self.firstname, self.lastname, self.privileges))
if __name__ == '__main__':
admin = Admin('zhang', 'san', ['can add post', 'can delete post', 'can ban user'])
admin.show_privileges()
作业4
"""
4.创建⼀一个Person类,添加⼀一个类字段⽤用来统计Perosn类的对象的个数
"""
class Person:
number = 0
def __init__(self):
Person.number += 1
def __del__(self):
Person.number -= 1