python类的了解及练习(快速了解)练习1 - 声明⼀个电脑类
属性:品牌、颜⾊、内存⼤⼩
⽅法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅式获取、修改、添加和删除 它的属性
b.通过attr相关⽅法去获取、修改、添加和删除它的属性
class Computer:
def__init__(self,brand,color,ram):
self.brand = brand
self.ram = ram
def play_game(self):
print("play game")
def write_code(self):
print("write code")
def view_video(self):
print("view video")
def__str__(self):
return str(self.__dict__)
# 传⼊⾃定义参数
computer_one=Computer('Acer','black','4G')
#a.对象点的⽅式获取、修改、添加和删除它的属性
print(computer_one)#{'brand': 'Acer', 'color': 'black', 'ram': '4G'}
computer_one.brand ="Lenovo"
lor ="yellow"
computer_one.ram ="8G"
print(computer_one)#{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G'}
computer_one.storage ="1T"
print(computer_one)#{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G', 'storage': '1T'}
del lor
print(computer_one)#{'brand': 'Lenovo', 'ram': '8G', 'storage': '1T'}
#b.通过attr相关⽅法去获取、修改、添加和删除它的属性
computer_two = Computer('Acer','black','4G')
a =getattr(computer_two,"brand")
b =getattr(computer_two,"color")
c =getattr(computer_two,"ram")
print(a,b,c)#Acer black 4G
setattr(computer_two,"brand","Lenovo")
setattr(computer_two,"color","blue")
setattr(computer_two,"ram","8G")
print(computer_two)#{'brand': 'Lenovo', 'color': 'blue', 'ram': '8G'}
delattr(computer_two,'brand')
print(computer_two)#{'color': 'blue', 'ram': '8G'}
setattr(computer_two,'strorage','2T')
print(computer_two)#{'color': 'blue', 'ram': '8G', 'strorage': '2T'}
练习2 - 声明⼀个⼈的类和狗的类:
狗的属性:名字、颜⾊、年龄
狗的⽅法:叫唤
⼈的属性:名字、 年龄、狗
⼈的⽅法:遛狗
a.创建⼈的对象名字叫⼩明,让他拥有⼀条狗 ,然后让⼩明去遛狗
class Dog:
def__init__(self,name,age,color):
self.name = name
self.age = age
def barking(self):
print("Dog barking")
class Person:
def__init__(self,name,age,dog):
self.name = name
self.age = age
self.dog = dog
def walking_the_dog(self,dog):
print("%s 正在遛⼀只名字为%s,颜⾊为%s的狗"%(self.name,dog.lor)) dog.barking()
dog1 = Dog("沙⽪",2,"黄⾊")
p1 = Person("⼩明",20, dog1)
p1.walking_the_dog(dog1)
'''
⼩明正在遛⼀只名字为沙⽪,颜⾊为黄⾊的狗
Dog barking
'''
练习3 - 声明⼀个矩形类
属性: 长、宽
⽅法:计算周长和⾯积
a.创建不同的矩形,并且打印其周长和⾯积
class Rectangle:
def__init__(self,length,width):
self.length = length
self.width = width
def perimeter(self):
return(self.length + self.width)*2
def area(self):
return self.length * self.width
rec1 = Rectangle(20,30)
print(rec1.perimeter(), rec1.area())
rec2 = Rectangle(10,20)
print(rec2.perimeter(), rec2.area())
练习4 - 创建⼀个学⽣类
属性:姓名,年龄,学号,成绩
⽅法:答到,展⽰学⽣信息
创建⼀个班级类: 属性:学⽣,班级名
⽅法:添加学⽣,删除学⽣,点名, 获取班级中所有学⽣的平均值, 获取班级中成绩最好的学⽣
class Student:
def__init__(self, name, age,id,score):
self.name = name
self.age = age
self.id=id
self.score=score
def answer(self):
print(self.__dict__)
def__str__(self):
return str(self.__dict__)
class Class:
def__init__(self, name, students:list):
self.name = name
self.students = students
def add_student(self,stu:Student):
self.students.append(stu)
def del_student(self,stu:Student):
ve(stu)
def call_student(self,stu:Student):
stu.answer()
def average_score(self):
sum=0
average=0
for stu in self.students:
sum+=stu.score
average=sum/len(self.students)
return averagepython新手代码练习
def best_student(self):
students.sort(key=lambda item:item.score,reverse=True)
return students[0].name
def__str__(self):
return str(self.__dict__)
stu1 = Student("⼩明",22,"stu01",98)
stu2 = Student("⼩李",24,"stu02",89)
stu3 = Student("⼩赵",28,"stu03",78)
stu4 = Student("⼩张",26,"stu04",93)
stu5 = Student("⼩王",22,"stu05",86)
stu6 = Student("⼩钱",26,"stu05",95)
students =[stu1, stu2, stu3, stu4, stu5]
python_class = Class("python1807", students)
print(python_class)
stu6 = Student("⼩钱",26,"stu05",95)
#添加学⽣
python_class.add_student(stu6)
print(python_class)
#删除学⽣
python_class.del_student(stu6)
print(python_class)
#求平均成绩
print(python_class.average_score())
#最优秀的学⽣
print(python_class.best_student())
#点名
python_class.call_student(stu1)
'''
结果为:
{'name':'python1807','students':[<__main__.Student object at 0x000002421A404B00>,<__main__.Student object at 0x000002421A404B38>,<__main_ _.Student object at 0x000002421A404B70>,<__main__.Student object at 0x000002421A404BA8>,<__main__.Student object at 0x000002421A404BE0>] }
{'name':'python1807','students':[<__main__.Student object at 0x000002421A404B00>,<__main__.Student object at 0x000002421A404B38>,<__main_ _.Student object at 0x000002421A404B70>,<__main__.Student object at 0x000002421A404BA8>,<__main__.Student object at 0x000002421A404BE0>, <__main__.Student object at 0x000002421A404D68>]}
{'name':'python1807','students':[<__main__.Student object at 0x000002421A404B00>,<__main__.Student object at 0x000002421A404B38>,<__main_ _.Student object at 0x000002421A404B70>,<__main__.Student object at 0x000002421A404BA8>,<__main__.Student object at 0x000002421A404BE0>] }
88.8
⼩明
{'name':'⼩明','age':22,'id':'stu01','score':98}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论