1
1.聲明?個電腦類: 屬性:品牌习寸、顏?、內(nèi)存?小 方法:打游戲傻工、寫代碼霞溪、看視頻
a.創(chuàng)建電腦類的對象,然后通過對象點的?方式獲取中捆、修改鸯匹、添加和刪除它的屬性
b.通過attr相關(guān)?方法去獲取、修改泄伪、添加和刪除它的屬性
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.創(chuàng)建人的對象小明,讓他擁有一條狗大黃津函,然后讓小明去遛大黃
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.創(chuàng)建?一個學(xué)?生類:
屬性:姓名,年齡尔苦,學(xué)號
方法:答到涩馆,展示學(xué)?生信息
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"] #打印除了簽到標(biāo)志的其他信息
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
創(chuàng)建一個班級類:
屬性:學(xué)生行施,班級名
方法:添加學(xué)生,刪除學(xué)生魂那,點名, 求班上學(xué)生的平均年齡
#[["name",age,Flag]["name1",age1,Flag1]] 數(shù)據(jù)結(jié)構(gòu)二維數(shù)組
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]]}