Day14作业

1.声明一个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computers:
    def __init__(self, made: str, color: str, memory: int):
        self.made = made
        self.color = color
        self.memory = memory

    def play_games(self):
        return '展示\n品牌:%s 颜色:%s 内存大小:%sGB 功能:打游戏' % (self.made, self.color, self.memory)

    def write_codes(self):
        return '展示\n品牌:%s颜色:%s 内存大小:%sGB 功能:写代码' % (self.made, self.color, self.memory)

    def watch_video(self):
        return '展示\n品牌:%s 颜色:%s 内存大小:%sGB 功能:看视频' % (self.made, self.color, self.memory)


computer1 = Computers('惠普', '黑色', 800)

#a.获取
print('a.获取颜色:color:%s' % computer1.color)
#b获取
print('b.获取出厂时间:', getattr(computer1, 'date', 20190630))

#a.修改
computer1.memory = 600
print('a.修改内存:', computer1.memory)
#b修改
print('b.修改品牌:', setattr(computer1, 'made', 'Apple'))


#a.添加
computer1.date = 190620
print('a.添加出厂时间:', computer1.date)
#b.添加
setattr(computer1, 'price', 3200)
print('b.添加价格:', computer1.price)

#a.删除
del computer1.color
print(computer1.color)
#b.删除
delattr(computer1, 'made')
print(computer1.made)

print(computer1.play_games())
print(computer1.write_codes())
print(computer1.watch_video())

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

class Person:
    def __init__(self, p_name: str, p_age: int, dog):
        self.p_name = p_name
        self.p_age = p_age
        self.dog = dog

    def walk_the_dog(self):
        print('%s遛%s'%(self.p_name, self.dog))


p = Person('小明', 16, '大黄')
p.walk_the_dog()


class Dog:
    def __init__(self, d_name: str, color: str, d_age: int):
        self.d_name = d_name
        self.color = color
        self.d_age = d_age

    def call_out(self):
        print('%s会嗷嗷叫' % self.d_name)

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

class Square:
    def __init__(self, radius:float):
        self.radius = radius

    def area(self):
        return '此圆面积为:%d' % float(3.14*self.radius**2)

    def perimeter(self):
        return '此圆周长为:%d' % float(2*3.14*self.radius)


square = Square(2.15)
print(square.area())
print(square.perimeter())

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