# 1. 声明一个电脑类
# 属性:品牌、颜色、内存大小
# 方法:打游戏、写代码、看视频
#
# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
# b.通过attr相关方法去获取、修改、添加和删除它的属性
#
class Computer:
def __init__(self, brand='Mac', color='silver', rom=8):
self.brand = brand
self.color = color
self.rom = rom
def play_computer_game(self):
print('打游戏')
def codeing(self):
print('写代码')
def watching(self):
print('看视频')
c1 = Computer('戴尔', 'black', 8)
# c1.play_computer_game()
print(c1.brand)
c1.color = 'red'
c1.price = 4000
del c1.rom
print(c1.__dict__)
print(getattr(c1, 'brand', '没有该品牌'))
setattr(c1, 'color', 'white')
setattr(c1, 'size', 'small')
delattr(c1, 'price')
print(c1.__dict__)
# 2.声明一个人的类和狗的类:
# 狗的属性:名字、颜色、年龄
# 狗的 法:叫唤
# 人的属性:名字、 年龄、狗
# 人的方法:遛狗
# a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗
#
class Dog:
def __init__(self, name='曾怀圣', color='神秘紫', age=22):
self.name = name
self.color = color
self.age = age
def call_out(self):
print('叫唤')
d1 = Dog()
class Person:
def __init__(self, name='胡建军', age=22, dog=''):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
print('遛狗')
self.dog.call_out()
p1 = Person('小明', 21, d1)
p1.walk_the_dog()
# 3.声明一个矩形类:
# 属性: 长、宽
# 方法:计算周长和面积
# a.创建不同的矩形,并且打印其周长和面积
#
class Rectangle:
def __init__(self, height: (int, float), width: (int,float)):
self.height = height
self.width = width
def area(self):
area = self.height*self.width
return area
r1 = Rectangle(2, 3)
r2 = Rectangle(22.1, 13)
print(r1.area(), r2.area())
# 4.创建一个学生类:
# 属性:姓名,年龄,学号,成绩
# 方法:答到,展示学生信息
# 创建一个班级类: 属性:学生,班级名
# 方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
class Student:
def __init__(self, name, age, stu_id, **score):
self.name = name
self.age = age
self.stu_id = stu_id
self.score = score
def speak(self):
print('%s到!' % self.name)
def show(self):
print('姓名:%s 年龄:%s 学号:%s 成绩:%s' % (self.name, self.age, self.stu_id, self.score))
class BanJi:
def __init__(self, name, student={}):
self.name = name
self.student = student
def stu_add(self, stu_name, student):
self.student[stu_name] = student
def show_stu(self):
for stu in self.student:
print(self.student[stu].__dict__)
def delete_stu(self, stu_name):
del self.student[stu_name]
def talk(self, stu):
stu.speak()
def max_stu(self, subject):
max_score = 0
stu = ''
for x in self.student:
if max_score < self.student[x].score[subject]:
max_score = self.student[x].score[subject]
stu = x
return max_score, x
def average(self, subject):
all_score = 0
average_score = 0
num = 0
for x in self.student:
all_score += self.student[x].score[subject]
num += 1
average_score = all_score/num
return average_score
stu1 = Student('徐畅', 21, 101, math=90)
stu2 = Student('胡建军', 22, 102, math=69)
stu3 = Student('曾怀圣', 22, 103, math=59)
cl1 = BanJi('幼稚园')
cl1.stu_add('胡建军', stu2)
cl1.stu_add('曾怀圣', stu3)
cl1.stu_add('徐畅', stu1)
print(cl1.max_stu('math'), cl1.average('math'))
cl1.show_stu()
stu1.show()
stu1.speak()
2018-10-17作业
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...