"""_ author _=陈泓羽"""
1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
print("==========================第1题=============================")
class Computer:
def __init__(self,brand:str,color:str,ram:int):
self.brand = brand
self.color = color
self.ram = ram
def play_game(self):
return "打游戏"
def write_code(self):
return "写代码"
def watch_video(self):
return "看视频"
print("==========================a问=============================")
c1 = Computer("联想","黑色",256)
print(c1.brand)
c1.brand = "戴尔"
print(c1.brand)
c1.color = "红色"
print(c1.color)
c1.user = "小明"
print(c1.user)
del c1.color
# print(c1.color)
print("==========================b问=============================")
print(getattr(c1,"ram"))
setattr(c1,"ram",128)
print(getattr(c1,"ram"))
setattr(c1,"size",14)
print(getattr(c1,"size"))
delattr(c1,"user")
# print(getattr(c1,"user"))
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
print("==========================第2题=============================")
class Dog:
def __init__(self,name,color,age):
self.name = name
self.color = color
self.age = age
def bark(self):
return "叫唤"
dog1 = Dog("大黄","黄色",8)
class Person:
def __init__(self,name,age,dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self,dog):
return ("%s溜%s"%(self.name,dog))
p1 = Person("小明",16,dog1.name)
print(p1.dog)
print(p1.walk_dog(dog1.name))
3.声明一个圆类,自己确定有哪些属性和方法
print("==========================第3题=============================")
class Circular:
def __init__(self,r):
self.radus = r
def perimeter(self):
return ("圆的周长为%.2f")%(2*3.14*self.radus)
def area(self):
return ("圆的面积为%.2f")%(3.14*self.radus**2)
c1 = Circular(5)
print(c1.perimeter())
print(c1.area())
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
print("==========================第4题=============================")
class Student:
def __init__(self,name,age,study_id):
self.name = name
self.age = age
self.study_id = study_id
def anwser(self):
print("答到")
def show_student_information(self):
# print("展示学生信息")
print(self.__dict__)
class Class:
def __init__(self,all_student,class_name = "python1903"):
self.student = all_student
self.name = class_name
def add_student(self,student1):
""""添加学生"""
self.student.append(student1)
def del_student(self,student):
"""删除学生"""
self.student.remove(student)
def average_age(self):
"""求平均年龄"""
sum1 = 0
for dict1 in self.student:
sum1 += dict1["age"]
average_age = sum1/len(self.student)
return average_age
c1 = Class([{"name":"小明","age":18,"study_id":"python1903001"}])
print(c1.student)
c1.add_student({"name":"小花","age":17,"study_id":"python1903005"})
c1.add_student({"name":"小陈","age":19,"study_id":"python1903009"})
print(c1.student)
print(c1.average_age())