day14-面向对象作业

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

class Conputer:
    def __init__(self,brand,color,memorysize):
        self.brand = brand
        self.color = color
        self.memorysize = memorysize

    def play():
        print('可以打游戏')

    def code():
        print('可以写代码')

    def watch():
        print('可以看视频')

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


class Conputer1:
    def __init__(self,brand,color,memorysize = '8G'):
        self.brand = brand
        self.color = color
        self.memorysize = memorysize


c1 = Conputer1('华硕','黑色')
c1.brand = '联想'
setattr(c1,'price', '贵的一批')
delattr(c1,'color')
print(c1.__dict__)


{'brand': '联想', 'memorysize': '8G', 'price': '贵的一批'}

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

print(getattr(c1, 'color', '红色'))

setattr(c1, 'color','白色')
print(c1.color)

delattr(c1,'color')
print(c1.__dict__)


红色
白色
{'brand': '联想', 'memorysize': '8G', 'price': '贵的一批'}

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

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

狗的方法:叫唤

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

人的方法:遛狗

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


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

    def liu(self):
        print( '溜%s' % (dog1.name) )
        return ('%s溜%s' % (p1.name,dog1.name))


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

    def jia(self):
        return('%s会汪汪汪' % (self.name))


p1 = Person(name='小明',)
dog1 = Dog(name='大黄',age=3,color='黄色')
print(p1.liu())
print(dog1.jia())



溜大黄
小明溜大黄
大黄会汪汪汪

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


class Circle:
    pi = 3.14
    def __init__(self,r = 1):
        self.r = r

    def func1(self):
        print(2 * Circle.pi * self.r)

    def func2(self):
        print(Circle.pi * self.r * self.r)

c1 = Circle(2)
c1.func2()

4.创建一个学生类:

属性:姓名,年龄,学号

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

创建一个班级类:

属性:学生,班级名

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


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

    def call(self):
        print('%s到,' % (self.name),self.__dict__)



class Banji:
    def __init__(self,student= '', classname = '神马班级'):
        self.student = []
        self.classname = classname


    def add(self):
        name = input('请添加姓名')
        age = input('年龄')
        num = input('学号')
        stu1 = Student(name,age,num)
        self.student.append(stu1)
        print('添加成功')

    def del_stu(self):
        del_name = input('输入要删除的姓名')
        count = len(self.student)
        for stu in self.student.copy():
            if stu.name == del_name:
                self.student.remove(stu)

    def dadao(self):
        for stu in self.student:
            print(stu.name)
            stu.call()

    def averrange(self):
        c = 0
        for stu in self.student:
            c += stu.age

        return c / len(self.student)




class1 = Banji()
for _ in range(5):
    class1.add()

class1.dadao()

你可能感兴趣的:(day14-面向对象作业)