2018-10-17作业

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

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

class Computer:
    """电脑类"""
    def __init__(self,brand,color,storage):
        self.brand=brand
        self.color=color
        self.storage=storage
    def play_game(self):
        print('play game')
    def write_code(self):
        print('write code')
    def watch_video(self):
        print('watch vedio')
c1=Computer('hp','black','2G')
print(c1.storage)
c1.color='red'
c1.productYear=2017
print(c1.productYear)
del c1.productYear
setattr(c1,'color','white')
print(c1.color)
setattr(c1,'productYear',2017)
print(c1.productYear)
delattr(c1,'productYear')

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

class Human:
    """人类"""
    def __init__(self,name,age=18,dog=''):
        self.name=name
        self.age=age
        self.dog=dog
    def walk_the_dog(self,dog):
        print('%s喜欢遛%s'%(self.name,dog.name))
class Dog:
    def __init__(self,name='小白',color='white',age=2):
        self.name=name
        self.color=color
        self.age=age
    def bark(self):
        print('%s喜欢叫唤'%self.name)

dog1=Dog()
human1=Human('小明',dog=dog1)
human1.walk_the_dog(dog1)
dog1.bark()

小明喜欢遛小白
小白喜欢叫唤

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

class rectangle:
    def __init__(self,length,width,unit=''):
        self.length=length
        self.width=width
        self.unit=unit
    def perimeter(self):
        print('长度%d%s,宽度%d%s的矩形周长是%d%s'%(self.length,self.unit,self.width,self.unit,(self.width+self.length)*2,self.unit))
    def area(self):
        print('长度%d%s,宽度%d%s的矩形面积是%d%s*%s'%(self.length,self.unit,self.width,self.unit,self.length*self.width,self.unit,self.unit))

rec1=rectangle(20,30,'cm')
rec1.perimeter()
rec1.area()
rec2=rectangle(10,32,'m')
rec2.perimeter()
rec2.area()

长度20cm,宽度30cm的矩形周长是100cm
长度20cm,宽度30cm的矩形面积是600cmcm
长度10m,宽度32m的矩形周长是84m
长度10m,宽度32m的矩形面积是320m
m

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

class Student:
    def __init__(self,name,age,study_id,score):
        self.name=name
        self.age=age
        self.study_id=study_id
        self.score=score
    def tick_name(self):
        print('姓名:%s,年龄:%d,学号:%s,成绩:%d'%(self.name,self.age,self.study_id,self.score))
##后面不会写了
class Class:
    def __init__(self,student=[],classname):
        self.student=student
        self.classname=classname
    def add_student(self,student):
        student=Student.__class__
        return student
    def del_student(self,student):
        student=Student.__class__
        del student
    def average_score(self,all_students):
        for student in all_students:
            sum(student.score)/len(student)

stu1=Student('sara',23,'201701',80)
stu2=Student('ann',17,201702,87)
stu1.tick_name()

你可能感兴趣的:(2018-10-17作业)