5.写一个类,封装所有和数学运算相关的功能
class MathMethod:
def __init__(self,num1=1,num2=1):
self.num1=num1
self.num2=num2
def choose_method(self,type):
if type=='+':
sum=self.add_method()
return sum
if type=='-':
sum=self.sub_method()
return sum
if type=='*':
sum=self.ride_method()
return sum
if type=='/':
sum=self.except_method()
return sum
if type=='%':
sum=self.savings_method()
return sum
if type=="//":
sum=self.rectify_method()
return sum
if type=="**":
sum=self.exp_method()
return sum
def add_method(self):
return self.num1+self.num2
def sub_method(self):
return self.num1-self.num2
def ride_method(self):
return self.num1*self.num2
def except_method(self):
if self.num2!=0:
return self.num1/self.num2
return False
def savings_method(self):
if self.num2!=0:
return self.num1%self.num2
return False
def rectify_method(self):
if self.num2!=0:
return self.num1//self.num2
return False
def exp_method(self):
return self.num1**self.num2
method=MathMethod(2,3)
print(method.choose_method('+'))
print(method.choose_method('-'))
print(method.choose_method('*'))
print(method.choose_method('/'))
print(method.choose_method('%'))
print(method.choose_method('//'))
print(method.choose_method('**'))
5
-1
6
0.6666666666666666
2
0
8
# 1.声明一个电脑类:
# 属性:品牌、颜色、内存大小
# 方法:打游戏、写代码、看视频
# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
# b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
__slots__ = ('brand','color','mem_size','money','address')
def __init__(self,brand,color,mem_size):
self.brand=brand
self.color=color
self.mem_size=mem_size
def play_game(self):
print('可以用来打游戏')
def write_code(self):
print('可以用来写代码')
def look(self):
print('可以播放电视')
ap_computer=Computer('苹果','red','8G')
print(ap_computer.color)
ap_computer.color='blue'
print(ap_computer.color)
ap_computer.address='美国'
print(ap_computer.address)
del ap_computer.address
# print(ap_computer.address)
print(ap_computer.__getattribute__('mem_size'))
print(getattr(ap_computer,'color'))
ap_computer.__setattr__('mem_size','16G')
print(ap_computer.mem_size)
setattr(ap_computer,'color','green')
print(ap_computer.color)
ap_computer.__setattr__('money','10000元')
print(ap_computer.money)
delattr(ap_computer,'money')
# print(ap_computer.money)
# ap_computer.__delattr__('money')
# print(ap_computer.money)
red
blue
美国
8G
blue
16G
green
10000元
2.声明一个人的类和狗的类:狗的属性:名字、颜色、年龄,狗的方法:叫唤,
人的属性:名字、年龄、狗,人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
__slots__ = ('dog_name',"color", 'dog_age')
def __init__(self, name,color, age):
self.dog_name=name
self.color=color
self.dog_age=age
def dog_call(self):
return '%s正在旺旺大叫'%(self.dog_name)
def __str__(self):
return 'dog_name:%s color:%s dog_age:%d'%(self.dog_name,self.color,self.dog_age)
class Person:
__slots__ = ('name','age','dog')
def __init__(self,name,age,dog=[]):
self.name=name
self.age=age
self.dog=dog
def get_dog(self,name,color, age):
dog1=Dog(name,color, age)
self.dog.append(dog1)
print('%s刚得到一条狗%s'%(self.name,dog1.dog_call()))
def walk_dog(self):
dog_name=self.dog[0].dog_name
return '%s正在遛他刚得到的%s'%(self.name,self.dog[0].dog_name)
p=Person('小明',20)
p.get_dog('大黄','yellow',3)
print(p.walk_dog())
3.声明一个矩形类:
属性:长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rect:
def __init__(self,height,width):
self.height=height
self.width=width
def perimeter(self):
return 2*(self.width+self.height)
def area(self):
return self.height*self.width
rect1=Rect(4,5)
print(rect1.perimeter(),rect1.area())
rect1=Rect(10,3)
print(rect1.perimeter(),rect1.area())
rect1=Rect(15,15)
print(rect1.perimeter(),rect1.area())
18 20
26 30
60 225
4.
创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名
class Student:
def __init__(self,name,age,id):
self.student_name=name
self.student_age=age
self.student_id=id
def answer(self):
print('%s到'%(self.student_name))
def check(self):
print('student_name:%s student_age:%s student_id:%s'\
%(self.student_name,self.student_age,self.student_id))
def __str__(self):
return 'student_name:%s student_age:%s student_id:%s'\
%(self.student_name,self.student_age,self.student_id)
class Class:
def __init__(self,grade,student=[]):
self.student=student[:]
self.grade=grade
def add_student(self,name,age):
id=''
try:
max=self.student[0].student_id
for item in self.student:
if max<item.student_id:
max=item.student_id
str1=int(max[-1])
str2=int(max[-2])
str3=int(max[-3])
if str1+1>9:
if str2+1>9:
str3+=1
else:
str2+=1
else:
str1+=1
id += 'py1805' + str(str3) + str(str2) + str(str1)
except IndexError:
id+='py1805001'
student1=Student(name,age,id)
self.student.append(student1)
student1.check()
def del_student(self):
list1=[]
del_student=input('请输入要删除学生的姓名或则id')
for item in self.student[:]:
# print(item.student_id,del_student)
id=item.student_id
if del_student==id:
self.student.remove(item)
return
elif del_student==item.student_name:
list1.append(item)
for x in range(len(list1)):
print(x,list1[x])
del_name=int(input('请输入选择'))
self.student.remove(list1[del_name])
for item in self.student:
print(item)
def check_student(self):
for item in self.student:
print(item)
check_name=input('输入姓名:')
for item in self.student:
if check_name==item.student_name:
student2=Student(item.student_name,item.student_age,item.student_id)
student2.answer()
c1=Class('1班')
c1.add_student('小明',20)
c1.add_student('小李',21)
c1.del_student()
c1.add_student('小廖',22)
print(c1.student[0])
c1.del_student()
print(c1.student[0])
c1.check_student()
D:\Python项目\Day12-面向对象\object\venv\word.py\Scripts\python.exe D:/Python项目/Day12-面向对象/object/作业.py
student_name:小明 student_age:20 student_id:py1805001
student_name:小李 student_age:21 student_id:py1805002
请输入要删除学生的姓名或则id小明
0 student_name:小明 student_age:20 student_id:py1805001
请输入选择0
student_name:小李 student_age:21 student_id:py1805002
student_name:小廖 student_age:22 student_id:py1805003
student_name:小李 student_age:21 student_id:py1805002
请输入要删除学生的姓名或则idpy1805002
student_name:小廖 student_age:22 student_id:py1805003
student_name:小廖 student_age:22 student_id:py1805003
输入姓名:小廖
小廖到
6.1.写一个班级类,属性:班级名、学生;功能:添加学生、删除学生、根据姓名查看学生信息,
展示班级的所有学生信息
class Class:
def __init__(self,grade,student=[]):
self.student=student
self.grade=grade
def add_student(self,name,age):
id=''
dict1={}
try:
max=self.student[0]['student_id']
for item in self.student:
if max<item['student_id']:
max=item['student_id']
str1=int(max[-1])
str2=int(max[-2])
str3=int(max[-3])
if str1+1>9:
if str2+1>9:
str3+=1
else:
str2+=1
else:
str1+=1
id += 'py1805' + str(str3) + str(str2) + str(str1)
except :
id+='py1805001'
dict1['student_name']=name
dict1['student_age'] = age
dict1['student_id'] = id
self.student.append(dict1)
def del_student(self):
list1=[]
del_student=input('请输入要删除学生的姓名或则id')
for item in self.student[:]:
# print(item.student_id,del_student)
id=item['student_id']
if del_student==id:
self.student.remove(item)
return
elif del_student==item['student_name']:
list1.append(item)
for x in range(len(list1)):
print(x,list1[x])
del_name=int(input('请输入选择'))
self.student.remove(list1[del_name])
for item in self.student:
print(item)
def check_all(self):
for item in self.student:
print(self.grade,'姓名:%s 年龄:%s id:%s' % (item['student_name'], item['student_age'], item['student_id']))
def check_student(self):
check_name=input('输入姓名:')
for item in self.student:
if item['student_name']==check_name:
return '姓名:%s 年龄:%s id:%s'%(item['student_name'],item['student_age'],item['student_id'])
c=Class('1班')
c.add_student('小明',20)
c.add_student('小李',22)
# print(c.check_student())
c.check_all()
c.del_student()
c.check_all()
1班 姓名:小明 年龄:20 id:py1805001
1班 姓名:小李 年龄:22 id:py1805002
请输入要删除学生的姓名或则id小明
0 {'student_name': '小明', 'student_age': 20, 'student_id': 'py1805001'}
请输入选择0
{'student_name': '小李', 'student_age': 22, 'student_id': 'py1805002'}
1班 姓名:小李 年龄:22 id:py1805002
网友评论