day14-code

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

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

class Computer:


    def __init__(self, brand, color, ram_size):
        self.brand = brand
        self.color = color
        self.ram_size = ram_size

    def play_game(self):
        print('用%s打游戏' % self.brand)

    def knock_code(self):
        print('敲代码')

    def watch_vidios(self):
        print('看视频')


computer = Computer('联想', '银', '4G')

# 获取
print(computer.brand)
print(getattr(computer, 'brand'))

# 修改

computer.color = '白'
print(computer.color)

setattr(computer, 'brand', '华硕')
print(computer.brand)

# 增
computer.size = 15.6
print(computer.size)

setattr(computer, 'price', 4500)
print(computer.price)

# 删除

del computer.size

delattr(computer, 'price')

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

狗的属性:名字、颜⾊、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大⻩,然后让小明去遛大黄

class Person:
    def __init__(self, name, age, dog):
        self.name = name
        self.age = age
        self.dog = dog
    def method(self):
        print('%s遛%s' % (self.name, self.dog))

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

dog = Dog('大黄', '白', 5)
person = Person('小明', 15, dog.name)

person.method()

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

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

    def area(self):
        return 3.14 * self.radius ** 2

    def perimeter(self):
        return 3.14 * self.radius * 2


circular = Circular(2)
print(circular.area())
print(circular.perimeter())

4.创建⼀个学⽣类:

属性:姓名,年龄,学号
方法:答到,展示学⽣信息

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

class Student:

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

    def stu_info(self):

        dict1 = {}
        dict1['name'] = self.name
        dict1['age'] = self.age
        dict1['stu_id'] = self.stu_id
        return dict1

    def answer(self):
        print('到')
        pass

class Class:

    dict_class = {
        'class1': [{'name': '小明', 'age': 15, 'stu_id': 'stu10001'},
                   {'name': '小红', 'age': 16, 'stu_id': 'stu10002'}
                   ],
        'class2': [{'name': '小值', 'age': 15, 'stu_id': 'stu20001'},
                   {'name': '小正', 'age': 14, 'stu_id': 'stu20002'}
                ]
    }

    def __init__(self, student, class_name):
        self.student = student
        self.class_name = class_name

    def add_stu(self):
        if self.class_name not in Class.dict_class:
            list1 = []
            Class.dict_class[self.class_name] = list1
            list1.append(self.student)
        else:
            Class.dict_class[self.class_name].append(self.student)
        return Class.dict_class

    def del_stu(self, dict1):
        for dict1 in Class.dict_class[self.class_name]:
            if self.student == dict1:
                Class.dict_class[self.class_name].remove(dict1)
        return Class.dict_class

    def roll_call(self, student):
        if student == Class.dict_class[self.class_name]:
            print(student)
            pass
        pass

    def ave_age(self):
        sum = 0
        count = 0
        for dict1 in Class.dict_class[self.class_name]:
            sum += dict1['age']
            count += 1
        return sum/count


stu1 = Student('zb', 18, 'stu10003')
a = stu1.stu_info()
class1 = Class(a, 'class1')

b = class1.add_stu()
print(b)
# {'class1': [{'name': '小明', 'age': 15, 'stu_id': 'stu10001'}, {'name': '小红', 'age': 16, 'stu_id': 'stu10002'},
# {'name': 'zb', 'age': 18, 'stu_id': 'stu10003'}],
# 'class2': [{'name': '小值', 'age': 15, 'stu_id': 'stu20001'}, {'name': '小正', 'age': 14, 'stu_id': 'stu20002'}]}

stu2 = Student('小明', 15, 'stu10001')
c = stu2.stu_info()

d = Class(c, 'class1')
print(d.del_stu(c))
# {'class1': [{'name': '小红', 'age': 16, 'stu_id': 'stu10002'}, {'name': 'zb', 'age': 18, 'stu_id': 'stu10003'}],
# 'class2': [{'name': '小值', 'age': 15, 'stu_id': 'stu20001'}, {'name': '小正', 'age': 14, 'stu_id': 'stu20002'}]}

print(d.ave_age())
# 17.0

stu = Student('x', 16, 'stu30001')
f = stu.stu_info()
e = Class(f, 'class3')

print(e.add_stu())
# {'class1': [{'name': '小红', 'age': 16, 'stu_id': 'stu10002'}, {'name': 'zb', 'age': 18, 'stu_id': 'stu10003'}], 
# 'class2': [{'name': '小值', 'age': 15, 'stu_id': 'stu20001'}, {'name': '小正', 'age': 14, 'stu_id': 'stu20002'}],
# 'class3': [{'name': 'x', 'age': 16, 'stu_id': 'stu30001'}]}

你可能感兴趣的:(day14-code)