day13-作业

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

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

    @classmethod
    def play_game(cls):
        print('打游戏')

    @classmethod
    def write_code(cls):
        print('写代码')

    @classmethod
    def watch_video(cls):
        print('看视频')


c1 = Computer()

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

# 获取
print(c1.brand)
# 添加
c1.brand = '华硕'
c1.color = '黑色'
c1.memory = '8G'
# 修改
c1.brand = '苹果'
# 删除
del c1.brand

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

# 获取
print(getattr(c1, 'brand', '外星人'))
# 添加
setattr(c1, 'brand', '华硕')
setattr(c1, 'brand', '黑色')
setattr(c1, 'brand', '8G')
# 修改
setattr(c1, 'brand', '华硕')
# 删除
delattr(c1, 'color')

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

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

狗的⽅方法:叫唤

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

人的方法:遛狗

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

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

    @classmethod
    def shout(cls):
        print('汪汪汪...')


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

    def walk_dog(self):
        print('%s在遛%s' % (self.name, self.dog.name))


d1 = Dog('大黄', 3, '黄色')
p1 = Person('小明', 18, d1)
p1.walk_dog()

运行:


image.png

3.声明⼀一个圆类:

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

    def area(self):
        import math
        area = (math.pi * self.radius ** 2)
        return '面积是%.2f' % area

    def peri(self):
        import math
        peri = 2 * math.pi * self.radius
        return '周长是%.2f' % peri


c1 = Circle(5)
print(c1.area(), c1.peri())

4.创建⼀一个学⽣生类:

属性:姓名,年龄,学号

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

创建⼀一个班级类:

属性:学⽣生,班级名

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

def open_read_files():
    with open('Pweson.json') as f:
        content = f.read()
        return content


def open_write_diles(content):
    with open('Person.json', 'w', encoding='utf-8') as f:
        f.write(content)


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

    @classmethod
    def answer(cls, name2):
        import json
        content = json.loads(open_read_files())
        for x in content:
            if name2 in x['name']:
                print('到!')
                return x


class Class1:
    def __init__(self, *stu, classname):
        self.stu = stu
        self.classname = classname

    @classmethod
    def add_stu(cls, s):
        import json
        content = json.loads(open_read_files())
        if not content:
            s = s.__dict__
            content.append(s)
            content = json.dumps(content)
            open_write_diles(content)
            content = json.loads(open_read_files())
            print(content)
            print('添加成功!')
        else:
            for x in content:
                if s.num not in x and content.index(x) == -1:
                    s = s.__dict__
                    content.append(s)
                    content = json.dumps(content)
                    open_write_diles(content)
                    content = json.loads(open_read_files())
                    print(content)
                    print('添加成功!')
                else:
                    print('账号已存在!')

    @classmethod
    def del_stu(cls, s):
        import json
        content = json.loads(open_read_files())
        if not content:
            print('无任何学生信息!')
        else:
            for x in content:
                if s.num1 in x:
                    content.remove(x)
                    open_write_diles(content)
                    print('删除成功!')
                else:
                    print('账号不存在!')

    @classmethod
    def call_the_roll(cls, name1):
        print('%s' % name1)
        s = Student()
        s.answer(name1)

    @classmethod
    def average(cls):
        import json
        content = json.loads(open_read_files())
        sum_age = 0
        for x in content:
            sum_age += x['age']
        return '平均年龄是%.2f' % sum_age

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