1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
# 1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self,trademark,color,memory):
self.trademark = trademark
self.color = color
self.memory = memory
def palygame(self):
print('you paly good!')
def writecode(self):
print('you write good!')
def watchvideo(self):
print('good movie!')
# a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
def main():
thinkpad = Computer('lenove','back','8g')
# 查
print(thinkpad.trademark,thinkpad.color,thinkpad.memory)
# 改
thinkpad.color = 'red'
#增
thinkpad.type = 'pad'
#删
del thinkpad.memory
# b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
print(getattr(thinkpad,'trademark',None)) #查
setattr(thinkpad,'color','black') #改
print(thinkpad.color)
setattr(thinkpad,'memory','8g') #增
print(thinkpad.memory)
delattr(thinkpad,'color') #删
if __name__ == '__main__':
main()
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('%s溜%s'%(self.name,self.dog))
class Dog:
def __init__(self,name='', color='黑色',age=1):
self.name = name
self.color = color
self.age = 10
def call_out(self):
print('%s能叫'%self.name)
dog = Dog('大黄','yellow',1)
person = Person('小明','8',dog.name)
person.walk_the_dog() #小明溜大黄
3.声明⼀一个圆类:
class Circle
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
def __init__(self,name,age,id):
self.name = name
self.age = age
self.id = id
def dadao(self,name):
print('%s到!'%self.name)
def show_info(self):
print(self.name,self.age,self.id)
class Class:
def __init__(self,classnum,student):
self.student = student
self.classnum = classnum
def add_stu(self):
stulist = []
stulist.append(self.student)
print('学生列表',stulist)
def add_stu(self):
stulist = []
stulist.append(self.student)
print('学生列表', stulist)
def dianming(self, name):
print('%s到!' % self.student)
////没做起。。。