1.声明个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Cumputer:
'''电脑,类的属性:品牌,颜色,内存大小,
方法:打游戏,写代码,看视频'''
def __init__(self, brand='', color='', memsize=''):
self.brand = brand
self.memsize = memsize
self.color = color
def game(self):
print('打游戏')
def code(self):
print('写代码')
def video(self):
print('看视频')
cpt1=Cumputer('opple','white','300G')
cpt2=Cumputer('dele','black','280G')
cpt3=Cumputer('lenovo','red','400G')
查属性值
print(cpt1.brand)
print(getattr(cpt1,'brand'))
修改属性值
cpt2.memsize='360G'
print(cpt2.memsize)
setattr(cpt3,'memsize','500G')
print(cpt3.memsize)
增加属性值
cpt2.price='800$'
print(cpt2.price)
删除属性的值
del cpt3.color
delattr(cpt2.memsize)
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
class Human:
'''人类,人的属性:名字、年龄、狗
人的方法:遛狗'''
def __init__(self, name='', age='', dog=''):
self.name = name
self.age = age
self.dog = dog
def game(self):
print('遛狗')
class Dog:
''' 狗,狗的属性:名字、颜色、年龄,
狗的方法:叫唤
'''
def __init__(self, name='', color='', age=''):
self.name = name
self.age = age
self.color = color
def game(self):
print('叫唤')
xiaoming=Human('小明','14','dahuang')
dahuang=Dog('大黄','黄色','2')
print('%s拥有%s' % (xiaoming.name,dahuang.name))
print('%s遛%s' % (xiaoming.name,dahuang.name))
a.创建人的对象小明,让他拥有⼀一条狗大黄,然后让小明去遛大黄
3.声明⼀一个圆类,自己确定有哪些属性和方法
class Circle:
'''圆,属性:半径、材料,
圆的方法:做轮子,做井盖,'''
def __init__(self, radial='', material=''):
self.radial = radial
self.material= material
def game(self):
print('做轮子')
def game2(self):
print('做盖子')
4.创建⼀一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建⼀一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Student:
''' 学生,属性:姓名、年龄,学号,
学生的方法:答到,展示学生信息 '''
def __init__(self, name='',age='',stu_id=''):
self.name = name
self.age= age
self.stu_id=stu_id
def game(self):
print('答到')
def game2(self):
print('展示学生信息')
class Class:
'''班级,属性:学生、班级名,
班级的方法:添加学生,删除学生,点名,求班里学生平均年龄'''
def __init__(self, class_name='',student=''):
self.class_name = class_name
self.student= student
def game(self):
print('添加学生')
def game3(self):
print('点名')
def game4(self):
print('求学生平均年龄')