Day14—作业

2018-09-07  本文已影响0人  C0mpass

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

class Computer:
    """电脑类"""
    def __init__(self, brand, memory='4G', color='黑色'):
        self.brand = brand
        self.memory = memory
        self.color = color

    def play_game(self):
        print('我可以打游戏')

    def write_code(self):
        print('我可以写代码')

    def watch_videos(self):
        print('我可以看视频')

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

c1 = Computer('联想')  # 获取品牌信息
print(c1.brand)
c1.memory = '8G'
print(c1.memory)  # 修改内存信息
c1.size = 14
print(c1.size)  # 添加尺寸属性
del c1.size
print(c1.size)  # 删除尺寸属性

输出结果为:

联想
8G
14
AttributeError: 'Computer' object has no attribute 'size'
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性。

getattr(c1, 'memory', 0)  # 获取内存信息
print(c1.memory)      
setattr(c1, 'brand', '戴尔')    # 修改品牌为戴尔
print(c1.brand)  
setattr(c1, 'size', 14)    # 增加尺寸属性
print(c1.size)
delattr(c1, 'size')    删除尺寸属性
print(c1.size)

输出结果为:

8G
戴尔
14
AttributeError: 'Computer' object has no attribute 'size'


2.声明⼀一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:叫唤
人的属性:名字、年龄、狗 人的方法:遛狗

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

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


class Person:
    """人类"""
    def __init__(self, name, age, pet):
        self.name = name
        self.age = age
        self.pet = pet

    def walk_dog(self):
        print('%s正在溜%s' % (self.name, self.pet))

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

d1 = Dog('大黄', '黄色', 3)
p1 = Person('小明', 18, d1.name)
print(p1.walk_dog())

输出结果:

小明正在溜大黄


3.声明⼀一个矩形类:
属性:长、宽 方法:计算周长和面积

class Rectangle:
    """矩形类"""
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length*self.width

    def perimeter(self):
        return 2*(self.length+self.width)

a.创建不同的矩形,并且打印其周长和面积

r1 = Rectangle(10,20)
r2 = Rectangle(15,25)
print('1矩形的长是%d,宽是%d,周长是%d,面积是%d' % (r1.length, r1.width, r1.perimeter(), r1.area()))
print('2矩形的长是%d,宽是%d,周长是%d,面积是%d' % (r2.length, r2.width, r2.perimeter(), r2.area()))

输出结果为:

1矩形的长是10,宽是20,周长是60,面积是200
2矩形的长是15,宽是25,周长是80,面积是375


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

class students:
    """学生类"""
    def __init__(self, name, age, id):
        self.name = name
        self.age = age
        self.id = id

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

    def display_information(self):
        print('姓名:%s, 年龄:%d,学号:%s' % (self.name, self.age, self.id))

创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名

class Class:
    """班级类"""
    def __init__(self, name):
        self.students = []
        self.name = name

    def add_student(self, student):
        self.students.append(student)

    def del_student(self, student_name):
        if not self.students:
            print('没有学生可以删除!')
        for item in self.students:
            if item['name'] == student_name:
                del item
        else:
            print('未找到该生!')

    def roll_call(self):
        if not self.students:
            print('没有学生!')
        for item in self.students:
            print(item['name']) 

5.写⼀一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)

class Calculation:
    pi = 3.141592654
    e = 2.718281828
    def plus(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num += item
        return first_num

    def minus(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num -= item
        return first_num

    def times(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num *= item
        return first_num

    def divided(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num /= item
        return first_num

    def remainder(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num %= item
        return first_num

    def divided(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num //= item
        return first_num

    def square(self, *num):
        first_num = num[0]
        for item in num[1:]:
            first_num **= item
        return first_num
上一篇下一篇

猜你喜欢

热点阅读