- 声明一个电脑类
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
"""
电脑类
"""
def __init__(self, trademark = "", color = "", memory = ""):
self.trademark = trademark
self.color = color
self.memory = memory
def play_game(self):
print("打游戏")
def write_code(self):
print("打代码")
def watch_video(self):
print("看电视")
c1 = Computer("神舟", "black", "128g")
# 查
print(c1.__dict__)
print(c1.trademark, c1.color, c1.memory)
# __getattribute__
print(c1.__getattribute__("trademark"), \
c1.__getattribute__("color"), \
c1.__getattribute__("memory"))
# getattr()
print(getattr(c1, "trademark", "属性错误"), \
getattr(c1, "color", "属性错误"), \
getattr(c1, "memory", "属性错误"))
# 增加/修改
c1.color = "yellow";
c1.addattr = "addattr"
print(c1.__dict__)
# __setattr__
c1.__setattr__("color", "blue");
c1.__setattr__("addcolor", "blue")
print(c1.__dict__)
# setattr()
setattr(c1, "color", "red");
setattr(c1, "add", "addred")
print(c1.__dict__)
# 删除
del c1.addattr
delattr(c1, "add")
c1.__delattr__("addcolor")
print(c1.__dict__)
结果
E:\python\类和对象\venv\Scripts\python.exe E:/python/类和对象/练习2.py
{'trademark': '神舟', 'color': 'black', 'memory': '128g'}
神舟 black 128g
神舟 black 128g
神舟 black 128g
{'trademark': '神舟', 'color': 'yellow', 'memory': '128g', 'addattr': 'addattr'}
{'trademark': '神舟', 'color': 'blue', 'memory': '128g', 'addattr': 'addattr', 'addcolor': 'blue'}
{'trademark': '神舟', 'color': 'red', 'memory': '128g', 'addattr': 'addattr', 'addcolor': 'blue', 'add': 'addred'}
{'trademark': '神舟', 'color': 'red', 'memory': '128g'}
Process finished with exit code 0
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的 法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗
class Person:
"""人类"""
def __init__(self,name="",age = 0,dog=""):
self.name = name
self.age = age
self.dog = dog
def walk_with_dog(self):
print("%s有一条%d岁的%s的狗,叫%s,%s正在遛它"\
% (self.name,self.dog.age,self.dog.color,self.dog.name,self.name))
class Dog:
"""狗类"""
def __init__(self,name="",age = 0,color=""):
self.name = name
self.age = age
self.color = color
def cry_out(self):
print(self.name+"正在叫唤\n汪 汪汪 汪汪汪汪")
dog1 = Dog("小黄",5,"黄色")
p1 = Person("小明",22,dog1)
p1.walk_with_dog()
dog1.cry_out()
结果
E:\python\练习\venv\Scripts\python.exe E:/python/练习/练习.py
小明有一条5岁的黄色的狗,叫小黄,小明正在遛它
小黄正在叫唤
汪 汪汪 汪汪汪汪
Process finished with exit code 0
3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
"""矩形"""
def __init__(self, length = 0, width = 0):
self.length = length
self.width = width
def perimeter(self):
num = self.width * 2 + self.length * 2
print("长宽为%.2f,%.2f的矩形的周长是%.2f" \
% (self.length, self.width, num))
def area(self):
num = self.length * self.width
print("长宽为%.2f,%.2f的矩形的面积是%.2f" \
% (self.length, self.width, num))
r1 = Rectangle(10, 20)
r2 = Rectangle(1.5, 2.5)
r1.perimeter()
r2.perimeter()
r1.area()
r2.area()
E:\python\类和对象\venv\Scripts\python.exe E:/python/类和对象/练习2.py
长宽为10.00,20.00的矩形的周长是60.00
长宽为1.50,2.50的矩形的周长是8.00
长宽为10.00,20.00的矩形的面积是200.00
长宽为1.50,2.50的矩形的面积是3.75
Process finished with exit code 0
4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
class Student:
"""学生"""
def __init__(self,name = "",age = 0,stu_id = 0,score = 0):
self.name = name
self.age = age
self.stu_id = stu_id
self.score = score
def check(self):
print(self.name+" 到!")
def __str__(self):
return str("姓名:%s,年龄:%d,学号:%d,成绩:%.2f"\
%(self.name,self.age,self.stu_id,self.score))
def show_student(self):
print("姓名:%s,年龄:%d,学号:%d,成绩:%.2f"\
%(self.name,self.age,self.stu_id,self.score))
class Myclass:
"""班级"""
next_student_id = 1
def __init__(self,students = [],class_name = ""):
self.students = students
self.class_name = class_name
# 添加学生
def add_student1(self):
name = input("请输入学生的姓名:")
age = input("请输入学生的年龄:")
score = input("请输入学生的成绩")
stu_id = self.next_student_id
self.next_student_id+=1
self.students.append(Student(name,age,stu_id,score))
def add_student(self,student):
self.students.append(student)
def delete_student(self,student):
self.students.remove(student)
def call_the_roll(self):
for s in self.students:
s.check()
def get_average(self):
sum1 = 0
for s in self.students:
sum1+=s.score
print("平均值为",sum1/len(self.students))
def get_max_score(self):
max1 = max(self.students,key = lambda item:item.score)
print("最高分是:",max1.score)
print("获得最高分的是:",max1)
def show_students(self):
for s in self.students:
s.show_student()
#或者 print(s)
class1 = Myclass([],"python1807")
s1 = Student("dj",1,1,80)
s2 = Student("ggdj",12,2,90)
s3 = Student("sdsdfdj",12,3,40)
class1.add_student(s1)
class1.add_student(s2)
class1.add_student(s3)
class1.show_students()
class1.get_average()
class1.get_max_score()
class1.delete_student(s1)
class1.show_students()
class1.call_the_roll()
结果
E:\python\类和对象\venv\Scripts\python.exe E:/python/类和对象/练习2.py
姓名:dj,年龄:1,学号:1,成绩:80.00
姓名:ggdj,年龄:12,学号:2,成绩:90.00
姓名:sdsdfdj,年龄:12,学号:3,成绩:40.00
平均值为 70.0
最高分是: 90
获得最高分的是: 姓名:ggdj,年龄:12,学号:2,成绩:90.00
姓名:ggdj,年龄:12,学号:2,成绩:90.00
姓名:sdsdfdj,年龄:12,学号:3,成绩:40.00
ggdj 到!
sdsdfdj 到!
Process finished with exit code 0