- 1.声明一个电脑类:
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
代码和结果
class Computer:
def __init__(self,brand='',color='',memory=''):
self.brand = brand
self.color = color
self.memory = memory
def play(self):
print('打游戏!')
def write(self):
print('写代码!')
def see(self):
print('看视频')
computer1 = Computer('华硕','黑色','4G')#创建电脑类的对象
# 题目a
print(computer1.brand,computer1.color,computer1.memory)#获取属性
computer1.color = '灰色'
print(computer1.brand,computer1.color,computer1.memory)#修改属性
computer1.price = 4000
print(computer1.brand,computer1.color,computer1.memory,computer1.price)#增加属性
del computer1.price#删除属性
print('=======分割线=======')
# 题目b
print(getattr(computer1,'brand'),getattr(computer1,'color'),getattr(computer1,'memory'))#获取属性
setattr(computer1,'brand','小米')
print(computer1.brand)#修改属性brand
setattr(computer1,'price','4000')
print(computer1.price)#增加属性price
delattr(computer1,'price')#删除属性price
D:\千锋培训作业\第三周作业\day12-作业\venv\Scripts\python.exe D:/千锋培训作业/第三周作业/day12-作业/work/01.py
华硕 黑色 4G
华硕 灰色 4G
华硕 灰色 4G 4000
=======分割线=======
华硕 灰色 4G
小米
4000
Process finished with exit code 0
- 2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:叫唤
人的属性:名字、年龄、狗 人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
代码和结果
class Dog:
def __init__(self,name='',color='',age=0):
self.name = name
self.color = color
self.age = age
def call(self):
print('%s:汪汪汪!' % self.name)
class Person:
def __init__(self,name='',age=0):
self.name = name
self.age = age
# None来表示对象的零值
self.dog = None
def stroll(self):
"""遛狗"""
if self.dog == None:
print('没狗,遛自己!')
return
print('%s遛%s' % (self.name,self.dog.name))
p1 = Person('小明',18)
p1.dog = Dog('大黄','yellow',3)
p1.stroll()
D:\千锋培训作业\第三周作业\day12-作业\venv\Scripts\python.exe D:/千锋培训作业/第三周作业/day12-作业/work/02.py
小明遛大黄
Process finished with exit code 0
- 3.声明一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
代码和结果
# 3.声明一个矩形类:
# 属性:长、宽 方法:计算周长和面积
# a.创建不同的矩形,并且打印其周长和面积
class Rect:
def __init__(self,length,width):
self.length = length
self.width = width
def c(self):
c = (self.length+self.width)*2
return c
def s(self):
s = self.length*self.width
return s
rect1 = Rect(2,5)
print('周长:',rect1.c())
print('面积:',rect1.s())
print('=======分割线=======')
rect2 =Rect(5,6)
print('周长:',rect2.c())
print('面积:',rect2.s())
D:\千锋培训作业\第三周作业\day12-作业\venv\Scripts\python.exe D:/千锋培训作业/第三周作业/day12-作业/work/03.py
周长: 14
面积: 10
=======分割线=======
周长: 22
面积: 30
Process finished with exit code 0
- 4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名
from random import randint
class Student:
def __init__(self,name='',age='',stuid=''):
self.name = name
self.age = age
self.stuid = 'py1805'+str(randint(0,50))
def answer(self):
print('%s到!'%self.name)
def show(self):
print('name:%s age:%d stuid:'%(self.name,self.age,self.stuid))
class Class:
def __init__(self,name='',):
self.class_name = name
self.students = []
def add_student(self,student=None):
self.students.append(student)
def del_student(self,name):
for student in self.students[:]:
if student.name == name:
self.students.remove(student)
def call(self):
for student in self.students:
print(student.name)
student.answer()
student1 = Student('xzq',15)
student2 = Student('qiyiou',16)
class1 = Class('py1805')
class1.add_student(student1)
class1.add_student(student2)
class1.call()
D:\千锋培训作业\第三周作业\day12-作业\venv\Scripts\python.exe D:/千锋培训作业/第三周作业/day12-作业/work/04.py
xzq
xzq到!
qiyiou
qiyiou到!
Process finished with exit code 0
- 5.写一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)
代码和结果
class Math:
def __init__(self,pi,e):
self.pi = pi
self.e = e
@classmethod
def add(cls,a,b):
s = a+b
print(s)
@classmethod
def minus(cls,a,b):
s = a - b
print(s)
@classmethod
def multiply(cls,a,b):
s = a*b
print(s)
@classmethod
def divide(cls,a,b):
s = a/b
print(s)
Math.add(1,2)
Math.minus(6,2)
Math.multiply(4,5)
Math.divide(8,4)
D:\千锋培训作业\第三周作业\day12-作业\venv\Scripts\python.exe D:/千锋培训作业/第三周作业/day12-作业/work/05.py
3
4
20
2.0
Process finished with exit code 0