day13 作业

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

class Computer:
    """电脑类"""
    def __init__(self,brand,color,memory):
        self.brand = brand
        self.color = color
        self.memory = memory
    def play_game(self):
        print("打游戏")
    def write_code(self):
        print("写代码")
    def see_movie(self):
        print("看视频")
cp1 = Computer("戴尔","yellow","16G")
cp1.play_game()
cp1.brand
cp1.color = "pink"
# del cp1.color
cp1.capan = "ssd"
print("添加:",cp1.capan)

attr方法:
class Computer:
    """电脑类"""
    def __init__(self,brand,color,memory):
        self.brand = brand
        self.color = color
        self.memory = memory
    def play_game(self):
        print("打游戏")
    def write_code(self):
        print("写代码")
    def see_movie(self):
        print("看视频")
print("获取",getattr(cp1,"color"),getattr(cp1,"brand"),getattr(cp1,"memory"))
setattr(cp1,"brand","lenovo")
setattr(cp1,"cipan","ssd")
setattr(cp1,"color","green")
setattr(cp1,"memory","2g")
print("添加修改后获取",getattr(cp1,"color"),getattr(cp1,"cipan"),getattr(cp1,"brand"),getattr(cp1,"memory"))
# delattr(cp1,"color")
# print("删除后获取",getattr(cp1,"color"),getattr(cp1,"brand"),getattr(cp1,"memory"))报错!

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

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

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

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

狗的⽅方法:叫唤

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

人的⽅方法:遛狗

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

class Dog:
    def __init__(self,name = " ",color = "",age = 0):
        self.name = name
        self.color = color
        self.age = age
    def shout(self):
        print("%s在叫唤"%self.name)
class Person:
    def __init__(self,name,age = 0):
        self.name = name
        self.age = age
        self.dog = None
    def play_with_dog(self):
        if self.dog:
            print("%s牵着%s在草地上玩"%(self.name,self.dog.name))
        else:
            print("没有狗,留自己")

p1 = Person("小明","18")
p1.dog = Dog("旺财","黄色","500")
p1.play_with_dog()

3.声明⼀一个圆类:

class Round:
    def __init__(self,radius):
        self.radius = radius
    def perimeter(self):
        return self.radius*3.14*2
    def area(self):
        return (self.radius)**2*3.14
r1 = Round(100)
perimeter1 = r1.perimeter()
area1 = r1.area()
print("周长为:%s,面积为:%s"%(perimeter1,area1))

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

属性:姓名,年龄,学号

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

创建⼀一个班级类:

属性:学⽣,班级名

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

a = []
aaa = {}
class Students:
    def __init__(self,information):
        self.information = information
    def reponse_infor(self):
        pass
    def show_student_infor(self):
        pass
name = input('请输入名称:')
age = input('请输入年龄:')
a.append(["年龄",age])
study_id = input('请输入学号:')
a.append(["学号",study_id])
aa = dict(a)
aaaa = {name:aa}
aaa.update(aaaa)
student = Students(aaa)

class Class:
    def __init__(self,student,class_name):
        self.student = student
        self.class_name = class_name
    def add_stu_inf(self):
        pass
    def changge_stu_info(self):
        pass
    def average_age(self):
        pass
class1 = Class(student,"class11")


import random
class Student():
    def __init__(self,name,age = 0,study_id = " "):
        self.name = name
        self.age = age
        self.study_id = study_id
        self.inclass = bool(random.randint(0,1))
    def reply(self):
        print("%s到"%self.name)
    def show_info(self):
        print("名字:%s,年龄:%s,学号:%s"%(self.name,self.age,self.study_id))


class Class:
    def __init__(self,name:str):
        self.name = name
        self.student = []
        self.__creat_id = (self.name + str(x).rjust(3, "0") for x in range(1, 101))
    def add_student(self):
        #输入信息
        name = input("姓名:")
        age = int(input("年龄:"))
        study_id = next(self.__creat_id)
        stu1 = Student(name,age,study_id)
        self.student.append(stu1)
    def del_student(self):
        is_exist = False
        del_name = input("请输入删除的学生的名字")
        for stu in self.student:
            if del_name == stu.name:
                is_exist = True
                stu.show_info()
                is_del = input("是否删除:Y/N?:")
                if is_del == "Y":
                    self.student.remove(stu)
                    print("删除成功")
        if not is_exist:
            print("这个学生不存在!")
    def naming(self):
        """点名"""
        for stu in self.student:
            print(stu.name)
            if stu.inclass:
                stu.reply()
    def average_age(self):
        sum1 = 0
        for stu in self.student:
            sum1 += stu.age
        return  sum1 / len(self.student)

    def show_student_info(self):
        """显示所有学生"""
        for stu in self.student:
            stu.show_info()

class1 = Class("py1808")
for i in range(3):
    class1.add_student()
#显示班级所有学生的信息
class1.show_student_info()
#删除学生
class1.del_student()
class1.add_student()
class1.show_student_info()

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