1.声明一个电脑类: 属性:品牌、颜色 、内存 方法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点的 式获取、修改、添加和删除它的属性 b.通过attr相关 法去获取、修改、添加和删除它的属性
class computer:
def __init__(self,brand,colour,memory):
self.brand = brand
self.colour = colour
self.memory = memory
def game(self):
print('打游戏')
def word(self):
print('敲代码')
def video(self):
print('看视频')
x = computer('苹果','银色','128G')
print(x.brand) #查看品牌
x.brand = '联想'
print(x.brand)#修改品牌名
x.size = '15寸'#增加
print(x.size)
del x.size#删除
print(getattr(x,'colour'))
setattr(x,'colour','黑色')
setattr(x,'size','15寸')
print(x.size)
delattr(x,'size')
x.game()
x.word()
x.video()
苹果
联想
15寸
银色
15寸
打游戏
敲代码
看视频
Process finished with exit code 0
2.声明 个 的类和狗的类:
狗的属性:名字、颜 、 龄 狗的 法:叫唤 的属性:名字、 龄、狗 的 法:遛狗 a.创建 的对象 明,让他拥有 条狗 ,然后让 明去遛
class Dog:
"""狗"""
def __init__(self, name1='', color1='', age1=0):
self.name = name1
self.color = color1
self.age = age1
def shout(self):
print('%s在汪汪叫!' % self.name)
class Person:
"""人"""
def __init__(self, name='', age=0):
self.name = name
self.age = age
self.dog = None # dog属性的值必须是Dog类的对象
def took_dog(self):
# 能遛狗的前提是自己有狗
if not self.dog:
print('没有🐶~溜自己吧!')
return
print('%s牵着%s在玩儿~' % (self.name, self.dog.name))
p1 = Person('小明')
p1.age = 18
p1.dog = Dog('大黄', '黄色', 2)
p1.took_dog()
小明牵着大黄在玩儿~
3.声明 个矩形类:
属性: 、宽 法:计算周 和 积 a.创建 同的矩形,并且打印其周 和 积
class Rectangle:
def __init__(self,long,wide):
self.long = long
self. wide = wide
# 对象的属性
def perimeter(self):
print('钜形周长是:',(self.long+self.wide)*2)
#
def area(self):
print('钜形的面积是:',(self.long*self.wide))
rect1 = Rectangle(55,60)
rect1.perimeter()
rect1.area()
钜形周长是: 230
钜形的面积是: 3300
88
4.创建 个学 类:
属性:姓名, 龄,学号 法:答到,展示学 信息 创建 个班级类: 属性:学 ,班级名 法:添加学 ,删除学 ,点名
class Student:
"""学生"""
def __init__(self, name, age=0, id=''):
self.name = name
self.age = age
self.id = id
def response(self):
"""答到"""
print('%s,到!' % self.name)
def show_info(self):
print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.id))
class Class:
"""班级"""
def __init__(self, name):
self.students = [] # 这个列表的元素是学生对象
self.name = name
self.__count = 0
def add_student(self):
"""添加学生"""
name = input('姓名:')
age = input('年龄:')
# 学号
self.__count += 1
id = 'stu' + str(self.__count).rjust(3, '0')
# 创建学生对象
stu = Student(name, int(age), id)
# 将学生保存到班级中
self.students.append(stu)
def del_student(self):
"""删除学生"""
del_name = input('请输入要删除的学生名字:')
is_del = False
# 遍历列表拿到的是学生对象
for stu in self.students[:]:
if stu.name == del_name:
self.students.remove(stu)
print('删除成功!')
is_del = True
if not is_del:
print('没有该学生!')
def call_names(self):
"""点名"""
for stu in self.students:
print(stu.name)
stu.response()
5.写 个类,封装所有和数学运算相关的功能(包含常 功能和常 值, 如:pi,e等)
class Math:
pi = 3.14159265358
e = 2.7
@staticmethod
def sum_double(num1, num2):
return num1 + num2
@classmethod
def circle_area(cls, r):
return cls.pi * r**2
class Addition:
def __init__(self,a,b):
self.a = a
self.b = b
def addition1(self):
print('和是:', (self.a+self.b))
ct1 = Addition(5,8)
ct1.addition1()
和是: 13
class Rectangle:
def __init__(self,long,wide):
self.long = long
self. wide = wide
def perimeter(self):
print('钜形周长是:',(self.long+self.wide)*2)
def area(self):
print('钜形的面积是:',(self.long*self.wide))
rect1 = Rectangle(44,60)
rect1.perimeter()
rect1.area()
钜形周长是: 208
钜形的面积是: 2640
6.1.写 个班级类,属性:班级名、学 ;功能:添加学 、删除学 、根据姓名查看学 信息,展示班级 的所有学 信息