Day14 作业
2019-08-08 本文已影响0人
风月辞寒
1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
"""______lxh______"""
class Computer:
__slots__ = ('brand', 'color', 'memory', 'size')
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
@staticmethod
def p_game():
print('打游戏!')
@staticmethod
def w_code():
print('写代码!')
@staticmethod
def w_video():
print('看视频!')
# .相关方法
computer1 = Computer('Lenovo', 'back', '512 GB')
print(computer1.brand, computer1.color, computer1.memory) # Lenovo back 512 GB
computer1.color = 'red' # 改
print(computer1.color) # red
computer1.size = '16寸' # 加
print(computer1.size) # 16寸
del computer1.brand # 删
# print(computer1.brand) # AttributeError: brand
# b.attr相关⽅方法
computer2 = Computer('Huawei', 'gray', '1T')
print(getattr(computer2, 'brand')) # 查
print(getattr(computer2, 'color'))
print(getattr(computer2, 'memory')) # Huawei gray 1T
setattr(computer2, 'brand', 'Lenovo') # 改
print(getattr(computer2, 'brand')) # Lenovo
delattr(computer2, 'memory') # 删
print(getattr(computer2, 'memory', '0B')) # 0B
setattr(computer2, 'size', '19寸') # 加
print(getattr(computer2, 'size')) # 19寸
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
__slots__ = ('name', 'color', 'age')
def __init__(self, name: str, color: str, age: int):
self.name = name
self.color = color
self.age = age
def bark(self):
print('%s: 汪汪汪!' % self.name)
class Person:
__slots__ = ('name', 'age', 'dog')
def __init__(self, name: str, age=0, dog=None):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
if self.dog:
print('%s遛%s' % (self.name, self.dog.name))
dog1.bark()
else:
print('没狗!')
dog1 = Dog('大黄', 'yellow', 3)
person1 = Person('小明', 20, dog1)
person1.walk_dog()
# 小明遛大黄
# 大黄: 汪汪汪!
3.声明⼀一个圆类,自己确定有哪些属性和方法
from math import pi
class Circle:
__slots__ = ('radius', 'girth', 'area')
def __init__(self, radius):
self.radius = radius
self.area = pi * radius ** 2
def c_girth(self):
return 2 * pi * self.radius
def c_area(self):
return pi * self.radius ** 2
circle1 = Circle(5)
print(circle1.c_girth()) # 周长 31.41592653589793
print(circle1.c_area()) # 面积 78.53981633974483
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
__slots__ = ('name', 'age', 'sno', 'sex', 'class_n')
def __init__(self, name=None, age=0, sno=None):
self.name = name
self.age = age
self.sno = sno
def replied(self):
print('%s: 到!' % self.name) # 答道
def message(self):
print(self.name, self.sno, self.age) # 打印学生信息
class Class:
__slots__ = ('stu', 'c_name')
def __init__(self, c_name, *stu):
self.stu = list(stu)
self.c_name = c_name
def add_stu(self, student): # 添加学生
self.stu.append(student)
def del_stu(self, student): # 删除学生
self.stu.remove(student)
def r_call(self): # 点名
for stu in self.stu:
print(stu.name)
def av_age(self): # 平均年龄
all_age = 0
for age in self.stu:
all_age += age.age
print(all_age / len(self.stu))
def main():
stu1 = Student('小明', age=19)
py1904 = Class('py1904', stu1)
stu2 = Student('小花', age=20)
py1904.add_stu(stu2)
py1904.r_call() # 小明 小花
py1904.av_age() # 19.5
if __name__ == '__main__':
main()