面向对象编程练习
- 声明一个电脑类
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer(object):
def __init__(self,type,color,ram):
self.type = type
self.color = color
self.ram = ram
def play_game(self):
print("打游戏")
def programme(self):
print("编程")
def watch_TV(self):
print("看视频")
def __str__(self):
return str(self.__dict__)
my_computer = Computer("lenovo","black","4G")
my_computer.play_game()
my_computer.programme()
my_computer.watch_TV()
print("*******对象.属性***********")
print(my_computer.type)
print(my_computer.ram)
my_computer.ram="8G"
print(my_computer.ram)
del my_computer.color
try:
print(my_computer.color)
except:
print("属性被删除了")
my_computer.price = "4000元"
print(my_computer.price)
print(my_computer)
print("*******attr相关方法***********")
print(getattr(my_computer,"type"))
print(getattr(my_computer,"color","没有该属性"))
setattr(my_computer,"color","black")
print(getattr(my_computer,"color","没有该属性"))
delattr(my_computer,"color")
print(getattr(my_computer,"color","该属性不存在"))
执行结果:
打游戏
编程
看视频
*******对象.属性***********
lenovo
4G
8G
属性被删除了
4000元
{'type': 'lenovo', 'ram': '8G', 'price': '4000元'}
*******attr相关方法***********
lenovo
没有该属性
black
该属性不存在
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的 法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗
class Dog(object):
def __init__(self,name,color,age:int):
self.name = name
self.age = age
self.color = color
def brak(self):
print("%s愤怒的叫唤"%self.name)
class Person(object):
def __init__(self,name,age:int,dog:Dog):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
print("%s正在和%s遛弯!"%(self.name,self.dog.name))
dog1 = Dog("大傻","白色",2)
dog1.brak()
p1 = Person("小明",18,dog1)
p1.walk_the_dog()
执行结果:
大傻愤怒的叫唤
小明正在和大傻遛弯!
3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Squareness(object):
def __init__(self,length:int,width:int):
self.length = length
self.width = width
def circumference(self):
return (self.width+self.length)*2
def area(self):
return self.length*self.width
s1 = Squareness(12,10)
s2 = Squareness(23,19)
print("周长:%s,面积:%s"%(s1.circumference(),s1.area()))
print("周长:%s,面积:%s"%(s2.circumference(),s2.area()))
执行结果:
周长:44,面积:120
周长:84,面积:437
4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
class Student(object):
id = 0
def __init__(self,age:int,name,score:int):
self.age = age
self.name = name
Student.id +=1
self.id = Student.id
self.score = int(score)
def answer(self):
print("姓名:%s 年龄:%s 学号:%s 成绩:%s"%(self.name,self.age,self.id,self.score))
class Class(object):
def __init__(self,name,student:list):
self.name = name
self.student = student
def add_student(self,student:Student):
self.student.append(student)
return self.student
def del_student(self,name:str):
for stu in self.student:
if name == stu.name:
self.student.remove(stu)
return self.student
def get_average_score(self):
sum = 0
for stu in self.student:
sum = sum +stu.score
return sum/len(self.student)
def get_best_score(self):
s = max(self.student,key= lambda item: item.score)
return s.name,s.score
def naming(self,name):
for stu in self.student:
if name == stu.name:
stu.answer()
stu1 = Student(23,"三十六贼",90)
stu1.answer()
student = [Student(23,"不要碧莲",90),Student(120,"宝儿姐",100),Student(25,"张灵玉",94),Student(21,"诸葛青",84)]
class1 = Class("一人之下",student)
print(len(class1.add_student(Student(28,"王也",89))))
print(len(class1.del_student("张灵玉")))
print(class1.get_average_score())
print(class1.get_best_score())
class1.naming("不要碧莲")
执行结果:
姓名:三十六贼 年龄:23 学号:1 成绩:90
5
4
90.75
('宝儿姐', 100)
姓名:不要碧莲 年龄:23 学号:2 成绩:90
网友评论