1.声明个电脑类: 属性:品牌、颜色、内存小 方法:打游戏、写代码、看视频
class Computer:
"""电脑类"""
def init(self,brand,color,memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print("打游戏")
def write_code(self):
print("写代码")
def see_movie(self):
print("看视频")
cp1 = Computer("戴尔","yellow","16G")
cp1.play_game()
cp1.brand
cp1.color = "pink"
del cp1.color
cp1.capan = "ssd"
print("添加:",cp1.capan)
attr方法:
class Computer:
"""电脑类"""
def init(self,brand,color,memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print("打游戏")
def write_code(self):
print("写代码")
def see_movie(self):
print("看视频")
print("获取",getattr(cp1,"color"),getattr(cp1,"brand"),getattr(cp1,"memory"))
setattr(cp1,"brand","lenovo")
setattr(cp1,"cipan","ssd")
setattr(cp1,"color","green")
setattr(cp1,"memory","2g")
print("添加修改后获取",getattr(cp1,"color"),getattr(cp1,"cipan"),getattr(cp1,"brand"),getattr(cp1,"memory"))
delattr(cp1,"color")
print("删除后获取",getattr(cp1,"color"),getattr(cp1,"brand"),getattr(cp1,"memory"))报错!
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
2.声明个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
def init(self,name,color,age = 2):
self.name = name
self.color = color
self.age = age
def shout(self):
print("狗叫唤")
dog1 = Dog("大大黄黄","黄黄")
class Person:
def init(self,name,age,dog):
self.name = name
self.age = age
self.dog = dog
def play_with_dog(self):
print("与小黄黄玩")
p1 = Person("小明","18",dog1)
p1.play_with_dog()
3.声明⼀一个圆类:
class Round:
def init(self,radius):
self.radius = radius
def perimeter(self):
return self.radius3.142
def area(self):
return (self.radius)*23.14
r1 = Round(100)
perimeter1 = r1.perimeter()
area1 = r1.area()
print("周长为:%s,面积为:%s"%(perimeter1,area1))
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建⼀一个班级类:
属性:学⽣,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
a = []
aaa = {}
class Students:
def init(self,information):
self.information = information
def reponse_infor(self):
pass
def show_student_infor(self):
pass
name = input('请输入名称:')
age = input('请输入年龄:')
a.append(["年龄",age])
study_id = input('请输入学号:')
a.append(["学号",study_id])
aa = dict(a)
aaaa = {name:aa}
aaa.update(aaaa)
student = Students(aaa)
class Class:
def init(self,student,class_name):
self.student = student
self.class_name = class_name
def add_stu_inf(self):
pass
def changge_stu_info(self):
pass
def average_age(self):
pass
class1 = Class(student,"class11")