-- coding: utf-8 --
"""
File Name: day12作业
Author : LPP
E-mail : l.piaopiao@qq.com
date: 2018/7/31
Change Activity:
2018/7/31:
"""
1.声明一个电脑类:
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand='DELL', color='black', memory='4Gb DDR4 2133MHz'):
self.brand = brand
self.color = color
self.memory = memory
def play_games(self):
print('%s玩扫雷一点都不卡'% self.brand)
def writing_codes(self):
print('%s写代码必备良品'% self.brand)
def watch_videos(self):
print('%s看西游记后传是真的卡')
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
computer = Computer('小米', '黑色')
print('computer.brand:',computer.brand)
print('getattr(computer', getattr(computer, 'brand'))
computer.brand = '宏碁'
setattr(computer, 'brand', '华硕')
print('computer.brand:',computer.brand)
print('getattr(computer, \'brand\')', getattr(computer, 'brand'))
computer.GPU = '1050Ti'
setattr(computer, 'CPU', 'i5-7300HQ')
print('computer.GPU:',computer.GPU)
print('getattr(computer, \'CPU\')', getattr(computer, 'CPU'))
del computer.memory
delattr(computer, 'color')
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:叫唤
人的属性:名字、年龄、狗 人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Person:
def __init__(self, name='', age=0, pet=None):
self.name = name
self.age = age
self.pet = pet
def walk_the_dog(self):
print('%s溜他的狗 -- %s' % (self.name, self.pet.name))
class Dog:
def __init__(self, name='', age=0, color='黑'):
self.name = name
self.age = age
self.color = color
def bark(self):
print('嗷~ 呜~~')
dog1 = Dog('大黄', 3)
p1 = Person('小明', 18, dog1)
p1.walk_the_dog()
3.声明一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
def __init__(self, width=0, height=0):
self.width = width
self.height = height
def perimeter(self):
print('周长: %.2f' % (2*(self.width+self.height)))
def area(self):
print('面积: %.2f' % (self.width*self.height))
r1 = Rectangle(6, 8)
r1.perimeter()
r1.area()
r2 = Rectangle(4, 6)
r2.perimeter()
r2.area()
4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名
class Student:
def __init__(self, name='', age=0, id='py1805001'):
self.name = name
self.age = age
self.id = id
def sign_in(self):
print('到!')
def show_info(self):
print('姓名:', self.name)
print('年龄:', self.age)
print('学号:', self.id)
class Class:
def __init__(self, name='py1805', students=[]):
self.name = name
self.students = students
def add_stu(self,student):
self.students.append(student)
def del_stu(self, id):
for stu in self.students:
if id == stu.id:
del stu
def call_the_roll(self):
for stu in self.students:
print(stu.name)
5.写一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)
class Calculation:
pi = 3.1416926
e = 2.718
# def __init__(self, a=0, b=0):
# self.a = a
# self.b = b
#
def plus(self, a, b):
return a+b
def multi(self, a, b):
return a*b
def divide(self, a, b):
if b:
return '除数不能为0'
else:
return a/b
def reduce(self, a, b):
return a-b
6.1.写一个班级类,属性:班级名、学生;功能:添加学生、删除学生、根据姓名查看学生信息,展示班级的所有学生信息
class Class:
def __init__(self, name='py1805', students=[]):
self.name = name
self.students = students
def add_stu(self,student):
self.students.append(student)
def del_stu(self, id):
for stu in self.students:
if id == stu.id:
self.students.remove(stu)
def find_stu(self,name):
for stu in self.students:
if stu.name == name:
print('姓名 学号 年龄\n',stu.name, stu.id, stu.age, sep=' ')
def show_all(self):
for stu in self.students:
print('姓名 学号 年龄\n', stu.name, stu.id, stu.age, sep=' ')