Day11作业-面向对象相关
2018-07-31 本文已影响67人
周zau1
1.声明一个电脑类:
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand='', color='', memory=''):
self.brand = brand
self.color = color
self.memory = memory
def play(self):
print('打游戏')
def wriht(self):
print('写代码')
def watch(self):
print('看视频')
computer = Computer('华硕', '黑色', '4GB')
# 查
print(computer.brand, computer.color, computer.memory)
print(getattr(computer, 'brand'), getattr(computer, 'color'), getattr(computer, 'memory'))
# 改
computer.color = '银色'
setattr(computer, 'memory', '8GB')
print(computer.brand, computer.color, computer.memory)
# 添
computer.inch = 15
setattr(computer, 'CPU', 'CORE i5')
print(computer.inch, computer.CPU)
# 删
del computer.inch
computer.__delattr__('CPU')
try:
print(computer.inch, computer.CPU)
except:
print('已删除')
华硕 黑色 4GB
华硕 黑色 4GB
华硕 银色 8GB
15 CORE i5
已删除
Process finished with exit code 0
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:叫唤
人的属性:名字、年龄、狗 人的方法:遛狗
a.创建人的对象小明,让他拥有一条大黄狗,然后让小明去遛大黄
class Person:
def __init__(self, name='', age='', dog=''):
self.name = name
self.age = age
self.dog = dog
def doing(self, thing='遛狗'):
print('%s在%s' % (self.name, thing))
class Dog:
def __init__(self, name='', color='', age=''):
self.name = name
self.color = color
self.age = age
def doing(self, thing='叫唤'):
print('%s在%s' % (self.name, thing))
person = Person('小明')
dog = Dog('大黄')
person.doing()
dog.doing()
小明在遛狗
大黄在叫唤
Process finished with exit code 0
3.声明一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rect:
def __init__(self, long=0, wide=0):
self.long = long
self.wide = wide
def rect(self):
print((self.long + self.wide) * 2)
print(self.long * self.wide)
rect = Rect(3, 4)
rect.rect()
print('===')
rect1 = Rect(5, 7)
rect1.rect()
14
12
===
24
35
Process finished with exit code 0
4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名
class Student:
def __init__(self, name='', age='', id=''):
self.name = name
self.age = age
self.id = id
def answer(self):
name = input('>>>')
if name == self.name:
print('%s已到!' % self.name)
def stu(self):
print(self.name, self.age, self.id)
stu = Student('jay')
stu.answer()
>>>jay
jay已到!
Process finished with exit code 0
class Student:
"""学生类"""
def __init__(self, name='', age=0):
self.name = name
self.age = age
def __str__(self):
return 'name:%s age:%d' % (self.name, self.age)
class Class:
"""班级类"""
def __init__(self, name='', students=[]):
self.class_name = name
self.students = students
def add_stu(self):
name = input('姓名:')
age = input('年龄:')
# 根据输入的信息创建学生对象
stu = Student(name, int(age))
# 添加学生
self.students.append(stu)
def del_stu(self):
name = input('姓名:')
for n in self.students:
if n.name == name:
self.students.remove(n)
5.写一个类,封装所有和数字运算相关的功能(包含常用功能和常用值,例如:pi,e等)
class Count:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def sum1(self):
print(self.x + self.y)
def les(self):
print(self.x - self.y)
num = Count(5, 3)
num.sum1()
num.les()
8
2
Process finished with exit code 0