Day14 作业

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

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

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

class Computer:

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

    @staticmethod
    def play_game(game):
        print('打游戏:%s'%game)

    def code(self):
        print('用%s代码'% self.brand)

    @staticmethod
    def video(video):
        print('看%s视频' % video)

对象.方法

computer1 = Computer('黑色', 'Mac', '8G')
# 获取
print(computer1.color, computer1.brand, computer1.ram)

# 修改
computer1.color = '白色'

# 添加
computer1.size = '20inch'

# 删除
del computer1.brand
print(computer1.color, computer1.size, computer1.ram)

attr相关方法

computer1 = Computer('黑色', 'Mac', '8G')
# 获取
print(getattr(computer1, 'color'))
# 修改
setattr(computer1, 'color', '白色')

# 添加
setattr(computer1, 'size', '20inch')

# 删除
print(computer1.color, computer1.size, computer1.ram)

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

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

狗的方法:叫唤

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

人的方法:遛狗

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

class Dog:
    def __init__(self, name, age, color):
        self.name = name
        self.age = age
        self.color = color

    def shout(self):
        print('%s:汪汪'%self.name)


class Person:

    def __init__(self, name, age, dog: 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))
        else:
            print('没有狗!')


def main():
    dog1 = Dog('大黄', 2, '黄色')
    person1 = Person('小明', 23, dog1)
    person1.walk_dog()

3.声明一个圆类:
方法一:

class Circle:
    pi = 3.14159265
    
    def __init__(self, r, x=0, y=0):
        self.r = r
        self.x = x
        self.y = y

    def area(self):
        return Circle.pi*self.r**2

    def perimeter(self):
        return Circle.pi*2*self.r

方法二:

class MyValueError(Exception):
    def __str__(self):
        return '当前属性不能复制!'


class Circle:
    pi = 3.14159265

    def __init__(self, r, x=0, y=0):
        self.r = r
        self.x = x
        self.y = y
        self._area = 0
        self._perimeter = 0

    @property
    def area(self):
        return Circle.pi*self.r**2

    @area.setter
    def area(self, value):
        raise MyValueError

    @property
    def perimeter(self):
        return Circle.pi*2*self.r

    @perimeter.setter
    def perimeter(self, value):
        raise MyValueError


def main():
    circle1 = Circle(5)
    print(circle1.area)
    # circle1.area = 2
    print(circle1.perimeter)

4.创建一个学生类:

属性:姓名,年龄,学号

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

创建一个班级类:

属性:学生,班级名

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

class Student:
    def __init__(self, name, age: int, number):
        self.name = name
        self.age = age
        self.number = number

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

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


class ClassStudent:
    def __init__(self, all_students : list, class_name):
        self.all_students = all_students
        self.class_name = class_name

    def add_stu(self, student: Student):
        self.all_students.append(student)

    def del_stu(self, stu_number: str):
        # self.all_students.pop(student)
        for stu in all_students:
            if stu.number == stu_number:
                all_students.remove(stu)
                return True
        else:
            return False

    def call_name(self, name: str):
        for stu in self.all_students:
            if stu.name == name:
                stu.here()
                break
        else:
            print('%s不在。'% name)

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

    def __repr__(self):
        return str([(stu.name,stu.age) for stu in all_students])


stu1 = Student('ququ', 12, '001')
stu2 = Student('jojo', 22, '002')
stu3 = Student('popo', 32, '003')


all_students = [stu1, stu2]
py_class = ClassStudent(all_students, 'python1809')
py_class.add_stu(stu3)
print(py_class.avg_age())
py_class.call_name('ququ')
py_class.del_stu('003')

print(py_class)

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