day13 - 类和对象(作业)

1.声明一个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关的方法去获取、修改、添加和删除它的属性

class Computer(object):
    def __init__(self,brand,color,memory):
        self.brand = brand
        self.color = color
        self.memory = memory
    def play_game(self):
        print('玩游戏')

    def write_code(self):
        print('写代码')

    def look_video(self):
        print('看视频')

# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性

#a创建和获取
computer1 = Computer('苹果', '白色', '8GB')
print(computer1.memory)
#a修改
computer1.memory = '16GB'
print(computer1.memory)
#a添加
computer1.size = '14inch'
print(computer1.size)
#a删除
del computer1.color
# print(computer1.color)  AttributeError

# b.通过attr相关的方法去获取、修改、添加和删除它的属性
#b创建和获取
computer2 = Computer('联想', '黑色', '4GB')
print(getattr(computer2, 'color'))
#b修改
setattr(computer2, 'brand', '华硕')
print(computer2.brand)
#b添加
setattr(computer2, 'size', '17')
print(computer2.size)
#b删除
delattr(computer2, 'color')
# print(computer2.color) # TypeError

2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象:小明,让他拥有一条狗:大黄,然后让小明去遛大黄

class Dogs(object):
    def __init__(self, name, color):
        self.name = name
        self.color = color
        self.age = 0
    def dog_call(self):
        print('汪汪汪!')


dog1 = Dogs('旺财', '白色')

class Persons(object):
    def __init__(self, name, age, dog):
        self.name = name
        self.age = age
        self.dog = dog
    def person_way(self):
        return print('%s遛%s' % (self.name, self.dog))


person1 = Persons('小明', 18, dog1.name)
person1.person_way()

3.声明一个圆类:

class Circle(object):
    pi = 3.1415926
    def __init__(self, r, x, y):
        self.r = r
        self.x = x
        self.y = y
    def get_d(self):
        return 2*self.r
    def get_area(self):
        return Circle.pi*self.r**2
    def circle_x(self):
        return abs(self.y - self.r)
    def circle_y(self):
        return abs(self.x - self.r)


circle1 = Circle(5, 10, 20)
print(circle1.get_d(), circle1.get_area(), circle1.circle_y(), circle1.circle

4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄

class Student(object):
    def __init__(self, name, age, student_id):
        self.name = name
        self.age = age
        self.student_id = student_id
    def response(self):
        print('%s到' % self.name)
    def show_message(self):
        print("""展示%s信息:
         姓名:%s
         年龄:%d
         学号:%d
        """ %(self.name,self.name, self.age, self.student_id))
# 创建一个班级类:
# 属性:学生,班级名
# 方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Class(object):
    def __init__(self, student, age, class_number):
        self.student = student
        self.class_number = class_number
        self.age = age
    def add_student(self):
        student_list.append(self.student)
        return student_list
    def del_student(self):
        student_list.remove(self.student)
        return student_list
    def call_name(self):
        print('呼叫%s' % self.student)
    def student_aver(self, age, n):
        return age / n


n = 0
ages = 0
student_list = []
while 1:
    name = input('请输入学生的姓名:')
    age = int(input('请输入学生的年龄:'))
    student_id = int(input('请输入学生的学号:'))
    student1 = Student(name, age, student_id)
    print(student1.age)
    student1.response()
    student1.show_message()
    n += 1
    class1 = Class(student1.name, student1.age, 'py1808')
    class1.add_student()
    print(student_list)
    class1.call_name()
    print(n)
    ages += student1.age
    print(class1.student_aver(ages,n))
    continue_number = input('请输入(1-3):')
    if continue_number == '1':
        continue
    elif continue_number == '2':
        class1.del_student()
    else:
        break

你可能感兴趣的:(day13 - 类和对象(作业))