day13

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

    class Computer(object):
        def __init__(self, color, memory, brand="华为"):
            self.color = color
            self.memory = memory
            self.brand = brand
        def play_games(self):
            print("打游戏喽")
        def code(self):
            print("写代码喽")
        def see_video(self):
            print("看视频喽")
    
    p1 = Computer('blank','512G')
    
    getattr(p1, 'color')
    print(p1.color)
    setattr(p1, 'color', 'red')
    print(p1.color)
    p1.size = '1024*2048'
    print(p1.size)
    delattr(p1, 'color')
    print(p1.color)

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

    class Person(object):
        def __init__(self, name, age, dog='小黑'):
            self.name = name
            self.age = age
            self.dog = dog
        def walk_dog(self):
            print("和%s遛圈圈去了" % self.dog)
    
    class Dog(object):
        def __init__(self, name, color, age):
            self.name = name
            self.color = color
            self.age = age
    
    p1 = Person('小明', 20)
    p2 = Dog('大黄', 'yellow', 2)
    p1.dog = p2.name
    print(p1.walk_dog())

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

    import math
    class Round(object):
        def __init__(self, radius):
            self.radius = radius
        def area(self):
            area1 = math.pi * (self.radius ** 2)
            return area1
        def perimeter(self):
            perimeter = math.pi * 2 * self.radius
            return perimeter
        
    p1 = Round(2)
    print(p1.area())
    print(p1.perimeter())

4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息

    class Student(object):
        def __init__(self, name, age, idcard):
            self.name = name
            self.age = age
            self.idcard = idcard
        def answer(self):
            print("学生的姓名是%s,年龄%s岁,学号是%s"%(self.name, self.age, self.idcard))
    
    
    p1 = Student("刘德华", 18, 123456)
    print(p1.answer())

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

    class Class(object):
        def __init__(self, classid, list1=[], list2=[]):
            self.list1 = list1
            self.list2 = list2
            self.classid = classid
        def add_student(self,name,age):
            self.list1.append(name)
            self.list2.append(age)
            return self.list1, self.list2
        def del_student(self,name,age):
            self.list1.remove(name)
            self.list2.remove(age)
            return self.list1, self.list2
    
        def call_rool(self, name):
            if name in self.list1:
                print("学生%s已在教室" % name)
            else:
                print("学生%s没在教室" % name)
    
        def ave_age(self):
            sum = 0
            n = 0
            for i in self.list2:
                sum += i
                n += 1
            return sum / n
    
    p1 = Class(1, ['王一'], [18])
    print(p1.add_student("王二", 18))
    print(p1.add_student("王三", 18))
    print(p1.add_student("王四", 17))
    print(p1.add_student("王五", 20))
   
    print(p1.del_student("王一", 18))
    p1.call_rool("王二")
    print(p1.ave_age())

你可能感兴趣的:(day13)