day13作业

import math
import json

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

class Computer:
    """
    关于电脑的相关特性
    """
    def __init__(self, trademark, color, inter ):
        self.trademark = trademark
        self.color = color
        self.inter = inter

    def playgame(self):
        return ('打游戏')

    def coding(self):
        return ('写代码')

    def video(self):
        return ('看视频')

c1 = Computer('apple','gray','8G')
print(c1.playgame())
print(c1.coding())
print(c1.video())

print(c1.trademark) # 查询


c1.color = 'black' # 修改
print(c1.color)

c1.hard_disk = '1T' #增加

del c1.hard_disk #删除



print(c1.__dict__)

print(getattr(c1,'trademark'))  #查询

setattr(c1,'inter','16G') #修改
print(c1.inter)

setattr(c1, 'hard_disk','1T')#增加
print(c1.hard_disk)


delattr(c1,'hard_disk') #删除
print(c1.__dict__)

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

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

狗的方法:叫唤

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

人的⽅方法:遛狗

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

class Dog:
    """
    犬科
    """
    def __init__(self, name,color,age):
        self.name = name
        self.color = color
        self.age = age
    def dog01(self):
        return ('%s这只狗正在叫唤'%(name))

dog2 = Dog('大黄','白色',3)

class Person:
    """
    人类
    """
    def __init__(self,name,age,dog):
        self.name = name
        self.age =age
        self.dog =dog
    def w_dog(self,w_dog):
        print(w_dog)
        return('小明正在遛%s'%(dog2.name))

man1 = Person('小明',18,dog2.name)
print(man1.w_dog(dog2))
print(man1.__dict__)

3.声明⼀个圆类:


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

    def area(self):
        return self.radius * math.pi

    def perimeter(self):
        return self.radius * 2 * math.pi

circle1 = Circle(6)
print(circle1.area())
print(circle1.perimeter())

4.创建⼀个学生类:

属性:姓名,年龄,学号

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

创建⼀个班级类:

属性:学⽣,班级名

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


class Grade:
    """
    班级
    """
    def __init__(self,name,grade):
        self.name = name
        self.grade = grade

    def add_stu(self):
        with open('stu_list.json', 'r',encoding='utf-8')as f:
            res = json.load(f)
            res.update(Student.s1.__dict__)
        with open('stu_list.json','w',encoding='utf-8')as f2:
            json.dump(res,f2)
        return f2



r1 = Grade('kaka',6)

r1.add_stu()


class Student:
    """学生们"""
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id =id
    def report(self):
        pass

s1 = Student("kaka",16,'stu001')

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