作业-day15-面向对象基础

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

class Computer:
    def __init__(self, brand, color, memsize):
        self.brand = brand
        self.color = color
        self.memsize = memsize

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

    @staticmethod
    def write_code():
        print('写代码')

    @staticmethod
    def watch_tv():
        print('看视频')


computer = Computer('华硕', '银', 15)
# 获取
print(computer.brand, computer.color, computer.memsize)  # 华硕 银 15
print(getattr(computer, 'brand'), getattr(computer, 'color'), getattr(computer, 'memsize'))  # 华硕 银 15

# 修改
computer.memsize = 12
computer.brand = '华为'
computer.color = '红'
print(computer.brand, computer.color, computer.memsize)  # 华为 红 12

setattr(computer, 'brand', '戴尔')
setattr(computer, 'color', '灰')
setattr(computer, 'memsize', 14)
print(computer.brand, computer.color, computer.memsize)  # 戴尔 灰 14

# 添加
computer.cpu = 'i78400'
print(computer.cpu)  # i78400

setattr(computer, 'xianka', 'GTX960')
print(computer.xianka)  # GTX960

# 删除
del computer.cpu
delattr(computer, 'xianka')

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

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

    def call(self):
        print('%s在叫唤' % self.name)


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

    def walk_the_dog(self):
        if self.dog:
            print("%s遛%s" % (self.name, self.dog.name))
        else:
            print('没有狗')


person = Person('小明', 15)
person.dog = Dog('大黄', '黄色', 2)
person.walk_the_dog()  # 小明遛大黄

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

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance(self, other):
        """
        求当前点到另外一个点的距离
        :param other: 另外一给点对象
        :return: 两个点之间的距离
        """
        a = self.x - other.x
        b = self.y - other.y
        return (a ** 2 + b ** 2) ** 0.5


class Circle:
    pi = 3.14

    def __init__(self, radius, center: Point):
        self.radius = radius
        self.center = center

    def area(self):
        print("圆的面积是%.2f" % (Circle.pi * self.radius ** 2))

    def perimeter(self):
        print("圆的周长是%.2f" % (2 * Circle.pi * self.radius))

    def circles_center_distance(self, other):
        """
        求两圆的圆心
        :param other:另外一个圆
        :return: 圆心距
        """
        return self.center.distance(other.center)


class Line:
    def __init__(self, start_point: Point, end_point: Point):
        self.start_point = start_point
        self.end_point = end_point

    def length(self):
        return self.start_point.distance(self.end_point)


line1 = Line(Point(100, 0), Point(0, 0))
print(line1.length())

4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息

创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄

class Student:
    def __init__(self, name, age, stu_id):
        self.name = name
        self.age = age
        self.stu_id = stu_id
        self.is_in_classroom = randint(0, 1)

    def replied(self):
        if self.is_in_classroom:
            print('%s到' % self.name)
        else:
            print('不在')

    def stu_info(self):
        print('姓名:{}, 年级:{}, 学号{}'.format(self.name, self.age, self.stu_id))


class Class:

    def __init__(self, class_name):
        self.students = []
        self.class_name = class_name

    def add_stu(self):
        stu_name = input("请输入学生姓名:")
        stu_age = int(input("请输入学生年龄:"))
        stu_id = input("请输入学生学号:")
        new_stu = Student(stu_name, stu_age, stu_id)
        self.students.append(new_stu)

    def del_stu(self):
        stu_id = input("请输入学生学号:")
        for stu in self.students[:]:
            if stu.stu_id == stu_id:
                self.students.remove(stu)
                break
        else:
            print('没有该学生')

    def call(self):
        for stu in self.students:
            print(stu.name)
            stu.replied()

    def average_age(self):
        count = len(self.students)
        sum1 = 0
        for stu in self.students:
            sum1 += stu.age
        ave_age = sum1 / count
        print(ave_age)

    # 让班级所有学生按年龄从小到大排序
    def sort_age(self):
        self.students.sort(key=lambda stu: stu.age)

    def show(self):
        for stu in self.students:
            stu.stu_info()


stu = Student('李四', 15, '123')

cl = Class('12班')
# print('添加学生')
# while 1:
#     cl.add_stu()
#     i = input('是否继续(1继续,任意键退出):')
#     if i != '1':
#         break
# print('删除学生:')
#
# cl.del_stu()
# print('点名')
# cl.call()
# print('平均年龄:')
for _ in range(5):
    cl.add_stu()
# cl.average_age()
cl.sort_age()
cl.show()

你可能感兴趣的:(作业-day15-面向对象基础)