1.声明 个电脑类: 属性:品牌、颜 、内存 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
'''电脑类'''
def __init__(self):
self.brand = '联想'
self.color = '黑色'
self.memory = '125G'
def func(self):
print('打游戏、写代码、看视频')
if __name__ == '__main__':
c1 = Computer()
c2 = Computer()
# 对象点方式
# 查
print(c1.func())
print(c1.brand,c1.color,c1.memory)
# 改
c1.brand = '华硕'
c1.color = '白色'
c1.memory = '1T'
print(c1.brand,c1.color,c1.memory)
# 增
c1.size = '13.5'
print(c1.size)
# 删
del c1.memory
# print(c1.memory) AttributeError: 'Computer' object has no attribute 'memory'
# attr相关方法
# 查
brand = c2.__getattribute__('brand')
color = c2.__getattribute__('color')
memory = c2.__getattribute__('memory')
print(brand,color,memory)
# 增
setattr(c2, 'size', 15)
print(c2.size)
# 删
delattr(c2, 'memory')
# print(c2.memory) AttributeError: 'Computer' object has no attribute 'memory'
# 改
setattr(c2,'color','白色')
print(c2.color)
2.声明一个人的类和狗的类:
狗的属性:名字、颜 、年龄
狗的叫法:
人的属性:名字、年龄、遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
'''狗的类'''
def __init__(self,name,color,age):
self.name ='大黄'
self.color = color
self.age = age
def dog_call(self):
print('汪汪')
class Person:
'''人的类'''
def __init__(self,name,age,dog ):
self.name = name
self.age = age
def func(self):
print('%s在公园遛%s'% (self.name,d.name))
d = Dog('大黄','黄色',3)
p = Person('小明',20,d.name)
print(p.func())
3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
'''矩形类'''
def __init__(self,long,width):
self.long = long
self.width =width
def area(self):
return self.long * self.width
def perimeter(self):
return (self.width + self.long) *2
r1 = Rectangle(10,5)
print(r1.area(),r1.perimeter()) # 50 30
r2 = Rectangle(7,10)
print(r2.area(),r2.perimeter()) # 70 34
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名
class Student:
def __init__(self,name,age,stu_id):
self.name = name
self.age = age
self.stu_id = stu_id
def func(self):
print('%s到'% (self.name))
def show(self):
print('姓名:%s,年龄:%s,学号:%s'% (self.name,self.age,self.stu_id))
class Class:
def __init__(self,class_name):
self.students = []
self.class_name = class_name
def add(self,student):
self.students.append(student)
print('学生:%s添加成功'% (student.name))
def delete(self,student_name):
for student in self.students:
if student.name == student_name:
self.students.remove(student)
print('%s删除成功'% (student_name))
def call_student(self):
for student in self.students:
student.func()
# 学生类
s1 = Student('张三',20,'001')
s2 = Student('张三丰',22,'002')
s3 = Student('李四',18,'003')
s4 = Student('天明',18,'004')
s1.show()
s2.show()
s3.show()
s4.show()
# 打印
'''
姓名:张三,年龄:20,学号:001
姓名:张三丰,年龄:22,学号:002
姓名:李四,年龄:18,学号:003
姓名:天明,年龄:18,学号:004
'''
# 班级类
c1 = Class('python1806')
print(c1.class_name)
# 添加学生
c1.add(s1)
c1.add(s2)
c1.add(s3)
c1.add(s4)
'''
学生:张三添加成功
学生:张三丰添加成功
学生:李四添加成功
学生:天明添加成功
'''
# 删除学生
c1.delete('张三')
# 点名
c1.call_student()
5.写一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)
class Math:
e = ... # type: float
pi = ... # type: float
# 将函数作为字典的值
def sub(*nums):
'''
累计求差
:param nums: 求差的数
:return: 差
'''
if not nums:
return 0
# 默认是第一个数
sum1 = nums[0]
for item in nums[1:]:
sum1 -= item
return sum1
# 函数作为函数的返回值
def operation(operator: str):
'''
累计求和
:param operator: 求和的数
:return: 和
'''
if operator == '+':
def my_sum(*nums):
sum1 = 0
for num in nums:
sum += num
# 将求和的函数返回
return my_sum()
def operation(operator: str):
'''
累计求积
:param operator: 求积的数
:return: 积
'''
if operator == '*':
def my_sum(*nums):
sum1 = 1
for num in nums:
sum1 *= num
print(sum1)
# 将求乘的函数返回
return my_sum
网友评论