Day14作业-2019/08/08

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

class Computer:
    def __init__(self, brand, color, memory_size):
        self.brand = brand
        self.color = color
        self.memory_size = memory_size

    def play_game(self):
        print('%s品牌可以打游戏' % self.brand)

    def write_code(self):
        print('%s品牌可以写代码' % self.brand)

    def watch_video(self):
        print('%s品牌可以看视频' % self.brand)


my_computer = Computer('联想', 'black', '8GB')
print(my_computer.brand, my_computer.color, my_computer.memory_size)
my_computer.play_game()
my_computer.write_code()
my_computer.watch_video()

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

print(my_computer.color)
my_computer.color = 'white'
print(my_computer.color)
my_computer.price = '54000'
print(my_computer.price)
del my_computer.price

b.通过attr相关方法去获取、修改、添加和删除它的属性

print(getattr(my_computer, 'color'))
setattr(my_computer, 'color', 'black')
print(my_computer.color)
setattr(my_computer, 'size', '24寸')
print(my_computer.size)
delattr(my_computer, 'size')

2.声明一个人的类和狗的类:

狗的属性:名字、颜色、年龄

狗的方法:叫唤

人的属性:名字、年年龄、狗

人的方法:遛狗

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

class Person:
    def __init__(self, name, age, his_dog):
        self.name = name
        self.age = age
        self.his_dog = his_dog

    def take_dog(self):
        print('%s在遛他的名字叫%s的狗' % (self.name, self.his_dog))


class Dog:
    def __init__(self, dog_name, color, dog_age):
        self.dog_name = dog_name
        self.color = color
        self.dog_age = dog_age

    def call_out(self):
        print('%s正在叫唤' % self.dog_name)


ming = Person('小明', 18, '大黄')
huang = Dog('大黄', 'white', 3)

ming.take_dog()

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

from math import *

class Circle:
    def __init__(self, radius, center_coordinates):
        self.radius = radius
        self.center_coordinates = center_coordinates

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

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


my_circle = Circle(10, (15, 20))
print(my_circle.radius, my_circle.center_coordinates)

print(my_circle.areas(), my_circle.perimeters())

4.创建一个学生类:

属性:姓名,年龄,学号

方法:答到,展示学生信息

创建一个班级类:

属性:学生,班级名

方法:添加学生,删除学生,点名, 求班上学生的平均年龄

class Student:
    def __init__(self, name, age, study_id):
        self.name = name
        self.age = age
        self.study_id = study_id

    def answer(self):
        print('%s 到' % self.name)

    def stu_information(self):
        print(self.name, self.age, self.study_id)


class Class:
    def __init__(self, class_name, students=[]):
        self.students = students
        self.class_name = class_name

    def add_student(self, name, age, study_id):
        new_student = Student(name, age, study_id)
        self.students.append(new_student)
        return self.students

    def del_student(self, del_id):
        for stu in self.students:
            if stu.study_id == del_id:
                self.students.remove(stu)
        return self.students

    def call_student(self):
        print('点名:')
        for stu in self.students:
            stu.answer()


    def average_age(self):
        sum1 = 0
        count = 0
        for stu in self.students:
            sum1 += stu.age
            count += 1
        return sum1/count


py_class = Class('python')
print(py_class.class_name)

py_class.add_student('小明', 15, '1904001')
py_class.add_student('张三', 16, '1904002')
py_class.add_student('李四', 14, '1904003')
py_class.call_student()

print(py_class.average_age())

py_class.del_student('1904001')


你可能感兴趣的:(Day14作业-2019/08/08)