day14-homework

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

class Computer:
    def __init__(self,brand,color,memory):
        self.brand = brand
        self.color = color
        self.memory = memory
    def play_game(self):
        print('play games')
    def write_codes(self):
        print('write codes')
    def look_video(self):
        print('look at video')

if __name__ == '__main__':
    computer = Computer('HP','white','2g')
    computer.play_game()
    print(computer.brand,computer.color,computer.memory)
    computer.brand = 'Apple'#修改
    computer.type = 'PC'#添加
    del computer.color #删除
    print(computer.brand, computer.type, computer.memory) #查看
    setattr(computer,'brand','华硕') #修改
    delattr(computer,'memory') #删除
    print(getattr(computer,'brand'),getattr(computer,'color','Black'),computer.__getattribute__('type')) #查看和添加

result:
play games
HP white 2g
Apple PC 2g
华硕 Black PC

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

class Dog:
    def __init__(self,name,color,age):
        self.color = color
        self.name = name
        self.age = age
    def voice(self):
        return(self.name+'-汪汪汪')
class Person:
    def __init__(self,name,age,dog):
        self.name = name
        self.age = age
        self.dog = dog
    def walk_dog(self,voice):
        print(self.name + '->遛狗->'+self.dog+'叫唤->'+voice)
if __name__ == '__main__':
    dog = Dog('大黄','粉色','3')
    person = Person('小明','10',dog.name)
    person.walk_dog(dog.voice())

result:
小明->遛狗->大黄叫唤->大黄-汪汪汪

声明一个矩形类:属性:长、宽 ⽅方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积

class Rectangle:
    def __init__(self,length,width):
        self.length = length
        self.width = width
    def circumference(self):
        return '周长:%.2f' % 2*(self.length+self.width)
    def area(self):
        return '面积:%.2f' % 2*self.length*self.width

创建一个学生类:属性:姓名,年龄,学号 ⽅方法:答到,展示学生信息
创建一个班级类:属性:学生,班级名 ⽅方法:添加学生,删除学生,点名

class Student:
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id = id
    def sign_in(self):
        print(self.name+'已到达')
    def show_student(self):
        print( {'name':self.name,'age':self.age,'id':self.id})
class Class:
    def __init__(self,class_name,students):
        self.class_name = class_name
        self.students = students
    def add_student(self,student):
        """
        添加学生到学生列表中
        :param student: 学生信息的字典
        :return:
        """
        self.students.apppend(student)
        print('添加学生成功.')
    def del_student(self,student_name):
        """
        传入学生的姓名后从学生列表中删除该学生
        :param student_name:
        :return:
        """
        for stu in self.students[:]:
            if student_name == stu['name']:
                self.students.remove(stu)
                print('删除成功')
                break
        print('删除失败.')
    def call_name(self):
        for stu in self.students:
            print(stu['name'],end=' ')

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

class Maths:
    pi = 3.141592653589793
    e = 2.718281828459045
    @classmethod
    def addition(cls,*number):
        """
        加法
        :param number:远算数元组
        :return:
        """
        return(sum(number))

    @classmethod
    def subtraction(cls,num,*number):
        """
        减法
        :param num:减数
        :param number:被减数元组
        :return:
        """
        return(num-sum(number))

    @classmethod
    def multiplication(cls,*number):
        """
        乘法
        :param number: 乘数元组
        :return:
        """
        count = 1
        for item in number:
            count *= item
        return count
    @classmethod
    def division(cls,num,*number):
        """
        除法
        :param num:除数
        :param number:被除数元组
        :return:
        """
        for item in number:
            num /= item
        return num

    @classmethod
    def exponentiation(cls, number,num):
        """
        乘方
        :param number: 底数
        :param num: 阶数
        :return:
        """
        count = 1
        if num > 0:
            for _ in range(num):
                count *= number
            return count

    @classmethod
    def factorial(cls, number,num):
        """
        阶乘
        :param number: 底数
        :param num:阶数
        :return:
        """
        count = 1
        if not num:
            return 1
        if num > 0:
            for n in range(1,num+1):
                count *= n
            return count

6.1.写一个班级类,属性:班级名、学生; 功能:添加学生、删除学生、根据姓名查看学生信息,展示班级的所有学生信息

class Class2:
    def __init__(self,class_name,students):
        self.class_name = class_name
        self.students = students
    def add_student(self,student):
        """
        添加学生到学生列表中
        :param student: 学生信息的字典
        :return:
        """
        self.students.apppend(student)
        print('添加学生成功.')
    def del_student(self,student_name):
        """
        传入学生的姓名后从学生列表中删除该学生
        :param student_name:
        :return:
        """
        for stu in self.students[:]:
            if student_name == stu['name']:
                self.students.remove(stu)
                print('删除成功')
                break
        print('删除失败.')
    def query_student(self,student_name):
        for stu in self.students:
            if student_name == stu['name']:
                print('找到该学生:%s' % stu)
                break
        print('没有找到该学生')
    def all_students(self):
        for stu in self.students:
            print(stu)

你可能感兴趣的:(day14-homework)