1.声明一个电脑类:属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self,logo='',color='',gb=''):
self.logo = logo
self.color = color
self.gb = gb
def play_game(self):
print('正在打游戏')
def write_code(self):
print('正在写代码')
def watch_video(self):
print('正在看视屏')
#a.
com1 = Computer('dell','red','8gb')
print(com1.color,com1.logo,com1.gb)
com1.gb ='16gb'
com1.price = 5000
del com1.color
print(com1.price,com1.logo,com1.gb)
#b.
print(getattr(com1,'logo',None))
com1.__setattr__('gb','32gb')
print(com1.gb)
com1.__setattr__('time','2018')
print(com1.time)
com1.__delattr__('time')
#print(com1.time)
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
age=16
name ='大黄'
color ='red'
def roll(self):
print('%s正在叫'%Dog.name)
dog =Dog()
dog.roll()
class Person:
def __init__(self,name='',age=0,dog=''):
self.name = name
self.age = age
self.dog= dog
def play_with_dog(self):
print('%s正在和%s玩耍'%(self.name,self.dog))
p1 = Person('小明',26,Dog.name)
p1.play_with_dog()
3.声明一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class rect:
def __init__(self,long=0,wide=0):
self.long = long
self.wide =wide
def maths_method(self):
perimeter = 2*(self.wide+self.long)
print('计算的矩形周长为:%s'%perimeter)
area = self.long*self.wide
print('计算的矩形面积为:%s'%area)
rct1 = rect(5,7)
rct1.maths_method()
rct2 = rect(8,6)
rct2.maths_method()
4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名
class Student:
def __init__(self,name=[],age=[],number=[]):
self.name =name
self.age = age
self.number = number
def __str__(self):
return('name:%s,age:%s,number%s,'%(self.name,self.age,self.number))
#展示学生
def display_stu(self):
print('name:%s,age:%s,number%s,'%(self.name,self.age,self.number))
#达到
def reply(self,name):
print('%s来了'%name)
class Class:
def __init__(self,name='',students=[]):
self.class_name = name
self.students = students
#增加学生
def add_student(self):
name = input('name:')
age = input('age:')
number = input('number:')
stu =Student(name,age,number)
self.students.append(stu)
#删除学生
def del_student(self,num):
del self.students[num]
#点名
def say(self,name):
print('%s来了没有'%name)
cls1 = Class('py1805')
cls1.add_student()
cls1.add_student()
cls1.add_student()
for cls in cls1.students:
print(cls)
cls1.del_student(0)
for cls in cls1.students:
print(cls)
5.写一个类
封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)
class Maths:
pi= 3.1415926
e = 2.71828
def __init__(self,x,y):
self.x =x
self.y = y
def plus(self):
print(self.x+self.y)
return self.x+self.y
def less(self):
print(self.x-self.y)
return self.x-self.y
def mul(self):
print(self.x*self.y)
return self.x*self.y
def division(self):
print(self.x/self.y)
return self.x/self.y
def power(self):
print(self.x**self.y)
return self.x**self.y
m1 = Maths(4,5)
m1.plus()
print(Maths.pi)
m1.power()
6.写一个班级类
属性:班级名、学生;功能:添加学生、删除学生、根据姓名查看学生信息,展示班级的所有学生信息
class Class1:
def __init__(self,name='',students={}):
self.class_name = name
self.students = students
#添加学生
def add_stu(self,name):
self.name = name
age =input('age')
sex = input('sex')
number = input('number')
self.students[self.name]= age,sex,number
# 删除学生
def del_stu(self, name):
self.name = name
del self.students[self.name]
# 根据姓名查看学生信息
def look_for_stu(self):
name =input('想要查找学生的信息')
self.name = name
print(self.students[self.name])
#展示班级的所有学生信息
def look_all(self):
print(self.students)
cls1 =Class1('1805')
cls1.add_stu('小明')
cls1.add_stu('小红')
cls1.del_stu('小红')
cls1.look_for_stu()
cls1.look_all()
网友评论