1
1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand:str, color:str, memory = 0):
self.brand = brand
self.color = color
self.memory = memory
def play_games(self):
print("The feature of playing game")
def coding(self):
print("The feature of coding")
def video(self):
print("The feature of watching video")
com1 = Computer("apple","white",memory=4096)
print(com1.__dict__)
print(com1.brand)#查
com1.memory = 2048#改
print(com1.memory)
com1.price = 12499#增
print(com1.price)
del com1.color#删除
print(com1.__dict__)
com2 = Computer("Thinkpad","black",memory=8192)
print(com2.__dict__)
print(getattr(com2,"brand","None"))#查
setattr(com2,"memory",4096)#改
print(getattr(com2,"memory","None"))
setattr(com2,"price",7899)#增
print(getattr(com2,"price","None"))
delattr(com2,"color")#删除
print(com2.__dict__)
{'brand': 'apple', 'color': 'white', 'memory': 4096}
apple
2048
12499
{'brand': 'apple', 'memory': 2048, 'price': 12499}
{'brand': 'Thinkpad', 'color': 'black', 'memory': 8192}
Thinkpad
4096
7899
{'brand': 'Thinkpad', 'memory': 4096, 'price': 7899}
2
声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
def __init__(self, name:str, color:str, age = 0):
self.name = name
self.color = color
self.age = age
def bellowing(self):
print("wang...wang...wang...")
class Person:
def __init__(self, name:str, dog:str, age = 0):
self.name = name
self.age = age
self.dog = dog
def take_the_dog(self):
print("%s遛%s"%(self.name,self.dog))
dog1 = Dog("大黄","yellow",age = 6)
print(dog1.__dict__)
dog1.bellowing()
boy = Person("小明","大黄", age = 18)
print(boy.__dict__)
boy.take_the_dog()
{'name': '大黄', 'color': 'yellow', 'age': 6}
wang...wang...wang...
{'name': '小明', 'age': 18, 'dog': '大黄'}
小明遛大黄
3
声明⼀一个圆类,自己确定有哪些属性和方法
import math
class Circle:
def __init__(self,radius):
self.radius = radius
def perimeter(self):
return 2*(self.radius)*math.pi
def area(self):
return (self.radius)**2 * math.pi
cir1 = Circle(3)
print("area:%.2f"%cir1.area())
print("perimeter:%.2f"%cir1.perimeter())
area:28.27
perimeter:18.85
4
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
class Student:
def __init__(self, name:str, age:int, id:str, Flag = True):
self.name = name
self.age = age
self.id = id
self.Flag = Flag
def show_message(self):
message = self.__dict__
del message["Flag"] #打印除了签到标志的其他信息
print(message)
def answer(self):
if self.Flag:
print("到")
else:
print("他没来")
stu1 = Student("xiaoming", 18, "python1902001", False)
stu1.answer()
stu1.show_message()
他没来
{'name': 'xiaoming', 'age': 18, 'id': 'python1902001'}
5
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
#[["name",age,Flag]["name1",age1,Flag1]] 数据结构二维数组
class Class:
def __init__(self, class_name:str, stu:list):
self.class_name = class_name
self.stu = stu
def append_stu(self, stu_name:list):
self.stu.append(stu_name)
def remove_stu(self,stu_name:str):
for i in range(len(self.stu)):
if stu_name == self.stu[i][0]:
del self.stu[i]
def answer(self,stu_name:str):
for i in range(len(self.stu)):
if stu_name == self.stu[i][0]:
if self.stu[i][2]:
print("%s到了"%self.stu[i][0])
else:
print("%s没来" % self.stu[i][0])
def ave(self):
res = 0
for i in range(len(self.stu)):
res += self.stu[i][1]
res = res / len(self.stu)
print("平均年龄:",res)
cla1 = Class("python1902",[])
cla1.append_stu(["xiaoming", 18, True])
cla1.append_stu(["xiaohua", 19, False])
print(cla1.__dict__)
cla1.ave()
cla1.answer("xiaoming")
cla1.answer("xiaohua")
cla1.remove_stu("xiaohua")
print(cla1.__dict__)
{'class_name': 'python1902', 'stu': [['xiaoming', 18, True], ['xiaohua', 19, False]]}
平均年龄: 18.5
xiaoming到了
xiaohua没来
{'class_name': 'python1902', 'stu': [['xiaoming', 18, True]]}