import random
1.声明一个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
"""说明文档:电脑类"""
# 属性
def __init__(self, brand, color, cpuSize='128G'):
self.brand = brand
self.color = color
self.cpuSize = cpuSize
# 方法
def playGame(self):
print('玩游戏')
def knockCode(self):
print('敲代码')
def watchTV(self):
print('看视频')
对象点的方式操作对象属性
computer1 = Computer('联想', '白色')
print('cpuSize:', computer1.cpuSize)
computer1.color = '黑色'
print('color:', computer1.color)
computer1.price = 5000
print('price:', computer1.price)
try:
del computer1.price
print('price:', computer1.price)
except AttributeError:
print('对象无此属性')
attr方法操作对象属性
computer2 = Computer('华硕', '黑色')
print('brand', getattr(computer2, 'brand'))
setattr(computer2, 'cpuSize', '256G')
print('cpuSize', getattr(computer2, 'cpuSize'))
setattr(computer2, 'price', 6000)
print('prie', getattr(computer2, 'price'))
try:
delattr(computer2, 'price')
print('price:', computer1.price)
except AttributeError:
print('对象无此属性')
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
"""文档说明:狗类"""
# 属性
def __init__(self, name, color='黄色', age=1):
self.name = name
self.color = color
self.age = age
# 方法
def speak(self):
print('汪汪汪...')
class Person:
"""文档说明:人类"""
# 属性
def __init__(self, name, dog, age=18):
self.name = name
self.dog = dog
self.age = age
# 方法
def walkTheDog(self):
print('%s正在遛他的狗%s' % (self.name, self.dog))
dog1 = Dog('大黄')
person1 = Person('小明', dog1.name)
person1.walkTheDog()
3.声明一个圆类,自己确定有哪些属性和方法
class Circle:
"""文档说明:圆类"""
# 属性
def __init__(self, radius):
self.radius = radius
# 方法
def side(self):
"""
求圆的周长
:return: 圆的周长
"""
result = 2*3.14*self.radius
return result
def area(self):
"""
求圆的面积
:return: 圆的面积
"""
result = 3.14*self.radius*self.radius
return result
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Student:
"""文档说明:学生类"""
# 属性
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
self.state = random.randint(0, 1)
# 方法
def answer(self):
if self.state == 1:
print(self.name+'到')
else:
print(self.name+'没到')
def showMessage(self):
print(self.name, self.age, self.id)
class Class:
"""文档说明:班级类"""
# 属性
def __init__(self, students: list, name):
self.students = students
self.name = name
# 方法
def addStu(self, student):
self.students.append(student)
def delStu(self, student):
for index in len(self.students):
if student == self.students[index]:
del self.students[index]
def present(self):
index = random.randint(0, len(self.students)-1)
return self.students[index]
def avgAge(self):
sum1 = 0
for item in self.students:
sum1 += item.age
avg = sum1/len(self.students)
return avg
student1 = Student('小明', 18, '20150901')
student2 = Student('小花', 16, '20150902')
student3 = Student('二狗', 17, '20150903')
student4 = Student('王大', 20, '20150904')
student5 = Student('毛毛', 19, '20150905')
student6 = Student('阿水', 17, '20150906')
student1.answer()
student2.showMessage()
class1 = Class([], '二年一班')
class1.addStu(student1)
class1.addStu(student2)
class1.addStu(student3)
class1.addStu(student4)
class1.addStu(student5)
class1.addStu(student6)
print('点名:', end='')
class1.present().answer()
print('平均年龄:', class1.avgAge())