1.声明一个电脑类: 属性:品牌、颜色、内存 方法:打游戏、写代码、看视频
class Computer:
def __init__(self,type,color,memery):
self.type=type
self.color=color
self.memery=memery
@classmethod
def play_game(cls):
print('play_game')
@classmethod
def write_code(cls):
print('write_code')
@classmethod
def watch_video(cls):
print('watch_video')
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
a
创建对象
com1=Computer('联想','黑色','512G')
# 获取
print(com1.type) #联想
# 修改
com1.type='华硕'
print(com1.type) #华硕
# 添加
com1.size=16
print(com1.size) #16
# 删除
del com1.type
# print(com1.type) # AttributeError: 'Computer' object has no attribute 'type'
b
com2=Computer('联想','黑色','512G')
# 获取
print(getattr(com2,'type')) #联想
# 修改
setattr(com2,'type','华硕')
print(getattr(com2,'type')) #华硕
# 添加
setattr(com2,'size',16)
print(getattr(com2,'size')) #16
# 删除
delattr(com2,'type')
# print(com2.type) # AttributeError: 'Computer' object has no attribute 'type'
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:狗叫唤
人的属性:名字、年龄、狗 人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Person:
def __init__(self,name,age,dog):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
print('正在遛狗中')
class Dog:
def __init__(self,name,age=5,color='黄色'):
self.name = name
self.age = age
self.color = color
def bark(self):
print('汪,汪,汪')
p1=Person('小明',18,'大黄')
dog1=Dog('大黄')
p1.walk_the_dog()
dog1.bark()
3.声明⼀一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
def __init__(self,length,width):
self.length=length
self.width = width
def perimeter(self):
perimeter=self.length*2+self.width*2
print('周长是%d'%(perimeter))
return perimeter
def area(self):
area=self.length*self.width
print('面积是%d'%(area))
return area
r1=Rectangle(30,20)
r1.perimeter()
r1.area()
r2=Rectangle(90,10)
r2.perimeter()
r2.area()
输出·
周长是100
面积是600
周长是200
面积是900
4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名
class Student:
students=[]
def __init__(self,name,age,id):
self.name=name
self.age = age
self.id = id
stu={'name':self.name,'age':self.age,'id':self.id}
Student.students.append(stu)
@classmethod
def show_stu(cls):
for x in Student.students:
print(x)
@classmethod
def answer(self):
is_arrive=int(input('到达请输入1:'))
if is_arrive==1:
return True
else:
return False
class Class:
def __init__(self,students,class_name):
self.students = students
self.class_name = class_name
@classmethod
def add_stu(cls,name,age,id):
stu=Student(name,age,id)
@classmethod
def del_stu(self):
id=int(input('请输入学号:'))
for x in Student.students[:]:
if x['id']==id:
Student.students.remove(x)
@classmethod
def ask_stu(self):
name=input('学生姓名:')
if Student.answer():
print('到')
else:
print('没到')
stu1=Student('aaa',19,12345)
stu2=Student('aab',14,12346)
Student.show_stu()
print(‘==========================’)
Class.add_stu('aac',18,123457)
Student.show_stu()
Class.del_stu()
Student.show_stu()
Class.ask_stu()
输出
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
=============================
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
{'name': 'aac', 'age': 18, 'id': 123457}
请输入学号:123457
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
学生姓名:aaa
到达请输入1:1
到
到
网友评论