2018-09-06课后作业(day14)

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):
        return '打游戏'
    def write_code(self):
        return '写代码'
    def watch_video(self):
        return '看视频'
x = Computer('lenovo','black','512G')
print('电脑可以:',x.play_game(),x.write_code(),x.watch_video())
# 获取
print(x.brand)    # lenovo
print(getattr(x,'color')) # black
# 修改
x.brand = 'apple' 
x.color = 'white' # white
print(x.brand,x.color) # apple white
# 添加
x.price = 5000
setattr(x,'thickness','30mm')
print(x.price,x.thickness) # 5000 30mm
# 删除
del x.brand
delattr(x,'color')
print(x.brand,x.color)  # AttributeError: 'Computer' object has no attribute 'brand'

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

class Dog:
    def __init__(self,name,color,age):
        self.name = name
        self.color = age
        self.age = age
    def dog(self):
        print('汪汪汪')
class Person:
    def __init__(self,name,age,dog):
        self.name1 = name
        self.age1 = age
        self.dog1 = dog
    def xiao_ming(self):
        print('遛狗')

if __name__ == '__main__':
    d1 = Dog('大黄','黄色','7')
    p1 = Person('小明',18,d1.name)
    print('%s遛%s'%(p1.name1,d1.name))

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

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 (self.length + self.width)*2
length1 = int(input('请输入长度:'))
width1 = int(input('请输入宽度:'))
rect = Rectangle(length1,width1)
print('矩形面积是:',rect.area())
print('矩形周长是:',rect.perimeter())

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

class Student:
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id = id
    def call_name(self,):
        print(self.name)
    def stu_info(self):
        print('到!我的名字是%s,今年%d岁,学号是%s'%(self.name,self.age,self.id))
s1 = Student('陈一',17,'001')
s2 = Student('王二',18,'002')
s3 = Student('张三',19,'003')
s4 = Student('李四',17,'004')
s1.call_name()
s1.stu_info()

class Class:
    def __init__(self,class_name,students):
        self.class_name = class_name
        self.students = []
    def add_stu(self,name):
        self.students.append(name)
    def del_stu(self,name):
        for stu1 in self.students:
            if stu1 == name:
                self.students.remove(name)
    def call_name1(self,name):
        for stu2 in self.students:
            if stu2 == name:
                print(stu2)

c1 = Class('python1806',[])
c1.add_stu('王二')
c1.add_stu('陈一')
c1.add_stu('张三')
print(c1.students)
c1.del_stu('王二')
print(c1.students)
c1.call_name1('张三')

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

class Math:
    def __init__(self,num1,num2):
        self.num1 = num1
        self.num2 = num2
    def operation(self):
         return (self.num1*self.num2),(self.num1/self.num2),(self.num1+self.num2),(self.num1-self.num2)
    def e(self):
        return (self.num1**2,self.num2**2)
    def pi(self):
        return (self.num1**2*3.14,self.num2**2*3.14)
m1 = Math(10,20)
print(m1.operation())
print(m1.e())
print(m1.pi())

你可能感兴趣的:(2018-09-06课后作业(day14))