Day14—类与对象 homework

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

class Computer:
    """电脑类"""
    def __init__(self, brand, colour, memory_size):
        self.brand = brand
        self.colour = colour
        self.memory_size = memory_size

    def game(self):
        print('打游戏')

    def code(self):
        print('写代码')

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


com1 = Computer('233', 'white', '8G')
# a.
print(com1.memory_size)
com1.brand = '322'
print(com1.brand)
com1.cpu = 'intel'
print(com1.cpu)
del com1.cpu
# b.
print(getattr(com1, 'brand'))
setattr(com1, 'brand', '233')
print(com1.brand)
setattr(com1, 'cpu', 'intel')
print(com1.cpu)
delattr(com1, 'cpu')

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

class Dog:
    """狗类"""
    def __init__(self, name, colour, age):
        self.name = name
        self.colour = colour
        self.age = age

    def dog_bark(self):
        print('汪!')


dog1 = Dog('大黄', 'black', 5)


class People:
    """人类"""
    def __init__(self, name, dog, age):
        self.name = name
        self.dog = dog
        self.age = age

    def walk_dog(self):
        print('%s去遛%s' % (self.name, self.dog.name))


person = People('小明', dog1, 12)
person.walk_dog()

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

class Circle:
    """圆"""
    pi = 3.14

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

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

    def area(self):
        return Circle.pi * self.radius**2


a = Circle(3)
print(a.perimeter())
print(a.area())

你可能感兴趣的:(Day14—类与对象 homework)