day14作业

1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频

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

b.通过attr相关⽅方法去获取、修改、添加和删除它的属性

class Computer:
    __slots__ = ("brand", "color", "ram", "size", "weight")

    def __init__(self, brand, color, ram):
        self.brand = brand
        self.color = color
        self.ram = ram

    def game(self):
        print("使用%s色的%s品牌电脑玩需要用%s内存才能玩的游戏" % self.color, self.brand, self.ram)

    def code(self):
        print("使用%s品牌的电脑写代码" % self.brand)

    def move(self):
        print("使用%s内存的电脑看视频" % self.ram)


stu1 = Computer("hp", "银色", "8G")
# 通过对象点获取
print(stu1.brand)
# 通过对象点方式修改
stu1.brand = "dell"
print(stu1.brand)

stu1.size = "13.3"
print(stu1.size)
# 通过对象点方式删除
del stu1.brand
print(stu1.brand)
# 通过attr方式获取
stu2 = input("请输入获取的属性:")
print(getattr(stu1, "%s" % stu2))
# 通过attr方式修改
setattr(stu1, "color", "黑色")
print(stu1.color)
# 通过attr方式删除
delattr(stu1, "ram")
print(stu1.ram)

2.声明⼀个人的类和狗的类:

狗的属性:名字、颜⾊色、年年龄

狗的⽅方法:叫唤

人的属性:名字、年年龄、狗

人的⽅方法:遛狗

a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄

class Person:
    def __init__(self, name, age, dog):
        self.name = name
        self.age = age
        self.dog = dog

    def walking_the_dog(self):
        print("%s在溜一条%s的狗" % (self.name, self.dog.dname))


class Dog:
    def __init__(self, dname, dcolor="red", dage="1"):
        self.dname = dname
        self.dcolor = dcolor
        self.dage = dage

    def canine(self):
        print("%s:汪汪汪" % dog1.dname)
#
#
dog1 = Dog("大黄", "black", "2")
dog = dog1
tur1 = Person("小明", "20", dog1)
tur1.walking_the_dog()

3.声明⼀一个圆类,自己确定有哪些属性和方法

from math import *


class Circle:
    def __init__(self, r):
        self.r = r

    def circumference(self):
        print("以%s为半径的圆,周长为%s" % (self.r, self.r ** 2 * pi))

    def area(self):
        return ("以%s为半径的圆,面积为%s" % (self.r, self.r ** 2 * pi))


r1 = Circle(5)
print(r1.area())

4.创建⼀一个学⽣

生类:

属性:姓名,年龄,学号

方法:答到,展示学⽣生信息

创建⼀一个班级类:

属性:学⽣生,班级名

方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄

class Student:
    def __init__(self, name, age=0, study_id='000'):
        self.name = name
        self.age = age
        self.study_id = study_id
        self.is_in_class = random.randint(0, 1)

    def respond(self):
        print('%s,到!' % self.name)

    def show(self):
        print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.study_id))


class Class:
    def __init__(self, name):
        self.students = []   
        self.name = name

        def func():
            num = 1
            while True:
                yield 'stu'+str(num)
                num += 1
        self.create_id = func()   

    def add_student(self):
        name = input('学生姓名:')
        age = int(input('学生的年龄:'))
        study_id = next(self.create_id)
        stu = Student(name, age, study_id)
        self.students.append(stu)

    def del_student(self):
        del_name = input('请输入要删除的学生的名字:')
        flag = False
        for stu in self.students[:]:
            if stu.name == del_name:
                flag = True
                stu.show()
                value = input('是否删除(y/n):')
                if value == 'y':
                    self.students.remove(stu)
        if not flag:
            print('没有该学生!')

    def call_name(self):
        for stu in self.students:
            print(stu.name)
            if stu.is_in_class:
                stu.respond()

    def avg_age(self):
        ages = 0
        for stu in self.students:
            ages += stu.age
        return ages / len(self.students)

    def show_all_students(self):
        print('=========%s的学生========='%self.name)
        for stu in self.students:
            stu.show()


c1 = Class('py1904')
c2 = Class('py1905')
c1.add_student()    # 001
c1.add_student()    # 002
c2.add_student()    # 001
c1.add_student()    # 003
c1.show_all_students()
c2.show_all_students()

你可能感兴趣的:(day14作业)