Day15作业
2019-01-11 本文已影响0人
圣咸鱼
- 声明一个电脑类
class Computer:
def __init__(self, brand, colour, memory_size):
self.brand = brand
self.colour = colour
self.memory_size = memory_size
def play_games(self):
print('打游戏')
def write_code(self):
print('写代码')
def watch_videos(self):
print('看视频')
computer1 = Computer('联想', 'black', '8G')
# 1.查
print(computer1.brand)
print(getattr(computer1, 'colour'))
# 2.改
computer1.brand = '华为'
print(computer1.brand)
setattr(computer1, 'brand', '苹果')
print(computer1.brand)
# 3.增
computer1.price = 10000
print(computer1.price)
setattr(computer1, 'system', 'windows')
print(computer1.system)
# 4.删
del computer1.price
# print(computer1.price) AttributeError: 'Computer' object has no attribute 'price'
delattr(computer1, 'system')
# print(computer1.system) AttributeError: 'Computer' object has no attribute 'system'
#联想
#black
#华为
#苹果
#10000
#windows
2.声明一个人的类和狗的类
class Dog:
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def cry(self):
print('汪汪汪')
dog1 = Dog('小黄', 5, '黄色')
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self, dog_name):
print('我去遛%s了'%(dog_name))
person1 = Person('小明', 18, dog1)
person1.walk_the_dog(person1.dog.name)
#我去遛小黄了
3.声明一个圆类:
class Circle:
def __init__(self, radius : float):
self.radius = radius
4.创建一个学生类和班级类
class Student:
def __init__(self, name, age, study_id, score):
self.name = name
self.age = age
self.study_id = study_id
self.score = score
def reply(self):
print('到')
def show_student_info(self):
return 'name: '+self.name+' age: '+self.age+' study_id: '+self.study_id
student1 = Student('小明', '18', '001', '90')
student2 = Student('小绿', '18', '002', '65')
student3 = Student('小花', '17', '003', '70')
student4 = Student('小红', '19', '004', '85')
students = [student1, student2, student3]
class My_class:
def __init__(self, stu, stu_class):
self.stu = stu
self.stu_class = stu_class
def add_student(self, stu):
students.append(stu)
def del_student(self, stu):
students.remove(stu)
def call_the_roll(self,stu_name):
print('来了吗%s'%(stu_name))
def my_max(self):
score_max = self.stu[0].score
the_name = self.stu[0].name
for stu in self.stu:
if stu.score > score_max:
score_max = stu.score
name = the_name
print(the_name, score_max)
def get_average_value(self):
score_sum = 0
for stu in self.stu:
score_sum += int(stu.score)
stu_number = int(len(self.stu))
score_average_value = score_sum/stu_number
return score_average_value
my_class1 = My_class(students, 'class01')
my_class1.add_student(student4)
for student in students:
print(student.show_student_info())
print('===================')
my_class1.del_student(student4)
for student in students:
print(student.show_student_info())
name1 = input('请输入要点名的人: ')
my_class1.call_the_roll(name1)
for student in students:
if student.name == name1:
student.reply()
my_class1.my_max()
print(my_class1.get_average_value())
#name: 小明 age: 18 study_id: 001
#name: 小绿 age: 18 study_id: 002
#name: 小花 age: 17 study_id: 003
#name: 小红 age: 19 study_id: 004
#===================
#name: 小明 age: 18 study_id: 001
#name: 小绿 age: 18 study_id: 002
#name: 小花 age: 17 study_id: 003
#请输入要点名的人: 小明
#来了吗小明
#到
#小明 90
#75.0