day14作业

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

class Computer:
    __slots__ = ('label', 'color', 'memory', 'hard_disk')

    def __init__(self, label, color, memory):
        self.label = label
        self.color = color
        self.memory = memory

    def play_game(self):
        print('用%s电脑打游戏' % self.label)

    def coding(self):
        print('用%s电脑写代码' % self.label)

    def watch_video(self):
        print('用%s电脑看视频' % self.label)



comp1 = Computer('华为', '银色', '8G')
comp2 = Computer('surface', '黑色', '4G')
comp3 = Computer('IBM', '白色', '8G')

"""打游戏"""
comp1.play_game()
comp2.play_game()
comp3.play_game()

print('================写代码==============')
comp1.coding()
comp2.coding()
comp3.coding()

print('================看视频==============')
comp1.watch_video()
comp2.watch_video()
comp3.watch_video()

print('=============增删改查===============')
print(comp1.label)
print(getattr(comp2, 'color'))
print(getattr( comp3, 'hard_disk', '512Gb'))

comp2.color = '香槟色'
print(comp2.color)
setattr(comp3, 'label', '联想')
print(comp3.label)
# del comp2.color
# print(comp2.color)  # AttributeError: color

delattr(comp1, 'memory')
print(comp1.memory)  # AttributeError: memory

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

class Person:
    __slots__ = ('name', 'age', 'dog_owns')

    def __init__(self, name, age, dog_owns):
        self.name = name
        self.age = age
        self.dog_owns = dog_owns

    def walk_the_dog(self):
        print('%s溜%s' % (self.name, self.dog_owns))

p1 = Person('小小明', '18', '大大黄黄')

p1.walk_the_dog()  # 小小明溜大大黄黄



class Dog:
    __slots__ = ('dog_name', 'color', 'dog_age')

    def __init__(self,dog_name, color, dog_age=5):
        self.dog_name = dog_name
        self.color = color
        self.dog_age = dog_age

    def bark(self):
        print('%s叫唤' % self.dog_name)

d1 = Dog('大大黄黄', '黄色')
d1.bark()  # 大大黄黄叫唤

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

import math

class Circle:
    def __init__(self, radius: int, center_location, color='blue'):
        self.radius = radius
        self.center_location = center_location
        self.color = color

    def area(self):
        print('面积等于', self.radius**2*math.pi)

    def perimeter(self):
        print('周长等于', self.radius*2*math.pi)

    def change_color(self):
        self.color = 'yellow'


c1 = Circle(3, (1, 0))
print(c1.color)
c1.area()  # 面积等于 28.274333882308138
c1.perimeter()  # 周长等于 18.84955592153876
c1.change_color()
print(c1.color)  # yellow

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

class Stu:
    def __init__(self, name, age, stu_id : str):
        self.name = name
        self.age = age
        self.stu_id = stu_id

    def response(self):
        print('到!我是%s,今年%d,学号是%s' % (self.name, self.age, self.stu_id))


s1 = Stu('王志峰', 18, '20180010')
s1.response()

class Class:
    def __init__(self, class_name, *stu):
        self.stu = stu
        self.class_name = class_name
        self.stus = []

    def add(self):
        self.stus.append(self.stu)
        print(self.stus)


s2 = Class('计算机系1班', '李雨轩')
s3 = Class('计算机系1班', '王子涵')
s2.add()
s3.add()

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