1.聲明?個(gè)電腦類: 屬性:品牌焕蹄、顏?淑翼、內(nèi)存?小 方法:打游戲腐巢、寫代碼、看視頻
a.創(chuàng)建電腦類的對象玄括,然后通過對象點(diǎn)的?方式獲取冯丙、修改、添加和刪除它的屬性
b.通過attr相關(guān)?方法去獲取遭京、修改胃惜、添加和刪除它的屬性
class Computer:
"""說明文檔:電腦類"""
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def play(self):
print('品牌%s,顏色%s哪雕,內(nèi)存%s船殉,能打游戲。' % (self.brand, self.color, self.memory))
def write_code(self):
print('品牌%s斯嚎,顏色%s利虫,內(nèi)存%s,能寫代碼堡僻。' % (self.brand, self.color, self.memory))
def watch_vedio(self):
print('品牌%s糠惫,顏色%s,內(nèi)存%s钉疫,能看視頻硼讽。' % (self.brand, self.color, self.memory))
if __name__ == '__main__':
computer1 = Computer('聯(lián)想', '黑色', '4G')
print(computer1.brand)
print(getattr(computer1, 'brand'))
computer1.brand = '蘋果'
print(computer1.brand)
setattr(computer1, 'brand', '外星人')
print(computer1.brand)
computer1.big = '45'
print(computer1.big)
setattr(computer1, 'size', 27)
print(computer1.size)
delattr(computer1, 'color')
del computer1.brand
2.聲明?個(gè)人的類和狗的類:
狗的屬性:名字、顏?色牲阁、年年齡
狗的?方法:叫喚
人的屬性:名字理郑、年年齡、狗
人的?方法:遛狗
a.創(chuàng)建?人的對象?小明咨油,讓他擁有?一條狗?大?黃,然后讓?小明去遛?大?黃
class Dog:
"""說明文檔:狗類"""
def __init__(self, name):
self.name = name
self.color = '黑色'
self.age = '5歲'
def yap(self):
print('叫喚')
class Person:
"""說明文檔:人類"""
def __init__(self, name, dog):
self.name = name
self.age = '18'
self.dog = dog
def walk_dog(self,):
print('%s有一條狗%s,然后%s去遛%s'% (self.name, self.dog, self.name, self.dog))
if __name__ == '__main__':
dog1 = Dog('大黃')
person1 = Person('小明', dog1.name)
person1.walk_dog()
3.聲明?一個(gè)圓類柒爵,自己確定有哪些屬性和方法
class Round1:
"""說明文檔:圓類"""
def __init__(self, radius):
self.radius = radius
self.pi = 'π'
def area1(self):
print('半徑為%s的圓役电,面積為%s'% (self.radius, str(self.radius**2)+self.pi))
if __name__ == '__main__':
p1 = Round1(3)
p1.area1()
4.創(chuàng)建?一個(gè)學(xué)?生類:
屬性:姓名,年齡棉胀,學(xué)號
方法:答到法瑟,展示學(xué)?生信息
創(chuàng)建?一個(gè)班級類:
屬性:學(xué)?生,班級名
方法:添加學(xué)?生唁奢,刪除學(xué)生霎挟,點(diǎn)名, 求班上學(xué)生的平均年齡
class Student:
"""說明文檔:學(xué)生類"""
def __init__(self, name, age, stu_num):
self.name = name
self.age = age
self.stu_num = stu_num
def answer(self):
print('到', self.__dict__)
class Class:
"""說明文檔:班級類"""
def __init__(self, *student,):
self.student = list(student)
self.class_name = '一班'
def add_student(self, name, age, stu_num):
dict1 = {'name': name, 'age': age, 'stu_num': stu_num}
self.student.append(dict1)
def del_student(self, name):
for i in self.student:
if i['name'] == name:
self.student.remove(i)
def check_name(self, student):
for i in self.student:
if i['name'] == student:
stu1 = Student(i['name'],i['age'],i['stu_num'])
stu1.answer()
break
else:
print('沒到')
def avg_student(self):
list2 = []
for i in self.student:
list2.append(i['age'])
avg = sum(list2)/len(list2)
print('平均值為', avg)
if __name__ == '__main__':
student1 = Student('小明', 18, '001')
student2 = Student('小馬', 19, '002')
class1 = Class(student1.__dict__, student2.__dict__)
class1.add_student('小黑', 20, '003')
print(class1.student)
class1.del_student('小馬')
print(class1.student)
class1.check_name('小明')
class1.check_name('小馬')
class1.check_name('小黑')
class1.check_name('小黑')
class1.avg_student()