1.申明一個(gè)電腦類(lèi):
屬性:品牌、顏色、內(nèi)存大小
方法:打游戲、寫(xiě)代碼、看視頻
a.創(chuàng)建電腦的對(duì)象,然后通過(guò)對(duì)象的方式,獲取,修改,添加和刪除的他的屬性
b.通過(guò)attr相關(guān)方法獲取、修改、添加、刪除它的屬性
class Computer:
def __init__(self,brand,color,memory):
self.brand=brand
self.color=color
self.memory=memory
print(self.brand,self.color,self.memory)
@classmethod
def play_game(cls):
print('打游戲')
@classmethod
def code(cls):
print('寫(xiě)代碼')
@staticmethod
def movie():
print('看視頻')
Computer.play_game()
Computer.code()
Computer.movie()
computer1=Computer('Lenovo',"black",'520G') #創(chuàng)建對(duì)象
#通過(guò)對(duì)象的方式
print("computer1的品牌是%s"%(computer1.brand)) #查看屬性
computer1.color='white' #修改屬性
computer1.size=(1020,800) #增加屬性
del computer1.memory #刪除屬性
#通過(guò)attr方式
print(getattr(computer1,'brand','Lenovo'))
computer1.__setattr__('color','gray') #修改屬性
print(computer1.color)
computer1.__setattr__('memory2',"512G") #添加屬性
print(computer1.memory2)
computer1.__delattr__('size') #刪除屬性
-------------------------------------------------------
2申明一個(gè)人的類(lèi)和一個(gè)狗的類(lèi):
狗的屬性:名字、顏色、年齡,狗的方法:叫換
人的屬性:名字、年齡、狗 ,人的方法:遛狗
a.創(chuàng)建人的對(duì)象小明,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Person:
def __init__(self,name1,age1,dog1):
self.name1=name1
self.age1=age1
self.dog1=dog1
def walk_dog(self):
print('遛%s'%(dog_1))
class Dog:
def __init__(self,name2,color2,age2):
self.name2=name2
self.color2=color2
self.age2=age2
def shout(self):
return '叫喚'
dog_1=Dog('大黃','黑','3 years')
person_1=Person('小明',18,dog_1)
print('%s有一只%s的%s狗,它的名字叫%s'%(person_1.name1,dog_1.age2,dog_1.color2,dog_1.name2))
person_1.walk_dog()
--------------------------------------------------------
3.申明一個(gè)矩形類(lèi):
屬性:長(zhǎng),寬 方法:計(jì)算周長(zhǎng)和面積
a.創(chuàng)建不同的矩形,并打印其周長(zhǎng)和面積
class Rectangle:
def __init__(self,longth,width):
self.longth=longth
self.width=width
def premiter(self):
return 2*(self.longth+self.width)
def aera(self):
return self.width*self.longth
rec1=Rectangle(40,70)
print('rec1的周長(zhǎng)是%.2f,面積是%.4f'%(rec1.premiter(),rec1.aera()))
rec2=Rectangle(30,60)
print('rec2的周長(zhǎng)是%.2f,面積是%.4f'%(rec2.premiter(),rec2.aera()))
rec3=Rectangle(25,40)
print('rec3的周長(zhǎng)是%.2f,面積是%.4f'%(rec3.premiter(),rec3.aera()))
------------------------------------------------------------
4.創(chuàng)建一個(gè)學(xué)生類(lèi):屬性:姓名,年齡,學(xué)號(hào) 方法:答到,展示學(xué)生信息,
創(chuàng)建一個(gè)班級(jí)類(lèi), 屬性:學(xué)生,班級(jí)名 方法:添加學(xué)生,刪除學(xué)生,點(diǎn)名
class Student:
def __init__(self,name,age,id):
self.name=name
self.age=age
self.id=id
def answer(self,call): #call=class1.call() #答到和展示學(xué)生信息
if self.name==call: #點(diǎn)名時(shí),學(xué)生判斷是否點(diǎn)到自己,
print("到",'\n',self.name,self.age,self.id) #展示學(xué)生信息
return 1
else:
print('此學(xué)生沒(méi)到')
return 0
class Class:
def __init__(self,class_name,students=[]):
self.class_name=class_name
self.students=students
def call(self,i): # i 所有學(xué)生的下標(biāo) ------點(diǎn)名
return self.students[i][name]
def append(self): #------------------------添加學(xué)生
name=input('請(qǐng)輸入姓名')
age=input('請(qǐng)輸入年齡')
id=input('請(qǐng)輸入學(xué)號(hào)')
stu1=Student(name,age,id)
cur_stu={'name':stu1.name,'age':stu1.age,'id':stu1.id}
self.students.append(cur_stu)
print('添加成功')
def delete(self): #----------------刪除學(xué)生
name = input('請(qǐng)輸入姓名')
id = input('請(qǐng)輸入學(xué)號(hào)')
for i in range(0,len(self.student)):
if self.students[i]['name']==name and self.students[i]['id']=id:
del self.students[i]
print('刪除成功')