day14 作业

2018-10-17  本文已影响0人  大漠判官1
  1. 声明一个电脑类
    属性:品牌、颜色、内存大小
    方法:打游戏、写代码、看视频
    a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
    b.通过attr相关方法去获取、修改、添加和删除它的属性
    """
class Computers:
    def __init__(self,brand = '',color = '',memory_size = ''):
        self.brand = brand
        self.color = color
        self.memory_size  = memory_size

    def computer_game(self):
        print('打游戏')
    def write_code(self):
        print('写代码')
    def see_video(self):
        print('看视频')

computer1 = Computers('联想','黄色','14')
computer2 =Computers('戴尔','黑色','15')
# 查
print(computer1.brand)
print(computer2.color)

# 改
computer1.memory_size = '15'
print(computer1.memory_size)
computer1.__setattr__('color','银白色')
print(computer1.color)

# 增
computer1.id ='2018-12312'
print(computer1.id)

# 删
del computer1.id
# print(computer1.id)


print('=======================================')
# 查
print(getattr(computer1,'color'))

#增
setattr(computer1,'factory','中山纬创')
print(computer1.factory)

# 改
setattr(computer1,'color','灰色')
print(computer1.color)

print('=================')
# 删
delattr(computer1.color)
print(computer1.color)

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

class Dog:
    def __init__(self,name = '',color ='',age = ''):
        self.name
        self.color
        self.age
    def dog_bark(self):
        print('叫唤')
class Person:
    def __init__(self,name :str,age : int,dog = Dog= None):
        self.name
        self.age
        self.dog
    def walk_dog(self):
        print('%s 在遛狗' % self.name)

person1 = Person('小明',12,'旺财')
print(Person.name)

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

    def shout(self):
        print('嗷嗷叫~')


class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age
        self.dog = None  # 注意:dog属性的类型应该是Dog

    def took_the_dog(self):
        if self.dog:
            print('%s正牵着%s在散步' % (self.name, self.dog.name))
        else:
            print('没有🐶,遛自己!')

    def beat(self):
        if not self.dog:
            print('没有🐶!')
            return
        print('%s在打自己的🐶' % self.name)
        self.dog.shout()

"""

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

class Rectangle:
    def __init__(self,length = '',width = ''):
        self.length = length
        self.width =width

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

    def area(self):
       area = self.length*self.width
       print(area)


rectangle1 = Rectangle(2,3)
print(rectangle1.perimeter())
print(rectangle1.area())

"""

"""

4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
"""

class Students:
    def __init__(self):
        self.name
        self.age
        self.id
        self.scode
    def replied(self):
        stu1 = {}
        print()

class Cla:
    def __init__(self):
        self.student
        self.cla_name
    def average(self):
class Student:
    def __init__(self, name='', age=0, score=0, study_id=''):
        self.name = name
        self.age = age
        self.study_id = study_id
        self.score = score

    def replied(self):
        print('%s,到!' % self.name)

    def show_message(self):
        print(self.__dict__)


import random


class Class:
    # 类字段
    __creat_id = ('python'+str(x).rjust(3, '0') for x in range(1, 101))

    def __init__(self, name):
        self.students = []
        self.name = name

    # 添加学生
    def add_student(self):
        # 输入学生信息
        name = input('姓名:')
        age = int(input('年龄:'))
        id = next(Class.__creat_id)
        score = random.randint(0, 100)
        # 创建学生对象
        stu = Student(name, age, score, id)
        self.students.append(stu)
        print('添加成功:')
        stu.show_message()

    # 删除学生
    def del_student(self):
        del_name = input('姓名:')
        count = len(self.students)   # 删除前学生的个数

        for stu in self.students.copy():
            if stu.name == del_name:
                self.students.remove(stu)

        if count == len(self.students):
            print('没有该学生!')

    def call(self):
        for stu in self.students:
            print(stu.name)
            # 学生答到
            stu.replied()

    def average_score(self):
        scores = 0
        for stu in self.students:
            scores += stu.score

        return scores/len(self.students)

    def most_excellent_student(self):
        return max(self.students, key=lambda stu: stu.score)


class1 = Class('python1807')
for _ in range(5):
    class1.add_student()
上一篇下一篇

猜你喜欢

热点阅读