1.聲明一個(gè)電腦類: 屬性:品牌、顏色、內(nèi)存大小棉饶。方法:打游戲、寫(xiě)代碼娘锁、看視頻
a.創(chuàng)建電腦類的對(duì)象,然后通過(guò)對(duì)象點(diǎn)的方式獲取饺鹃、修改莫秆、添加和刪除它的屬性
b.通過(guò)attr相關(guān)方法去獲取碎税、修改、添加和刪除它的屬性
class Computer:
def __init__(self, brand: str, color: str, memory_size: str):
self.brand = brand
self.color = color
self.memory_size = memory_size
def play_game(self):
print('打游戲馏锡!')
def write_code(self):
print('寫(xiě)代碼!')
def watch_tv(self):
print('看電視伟端!')
com1 = Computer('呆呆', '金', '8GB')
# a
# 1.獲取品牌名
print(com1.brand)
# 2.修改成紅色
com1.color = '紅'
print(com1.color)
# 3.添加新屬性:使用壽命
com1.life = 5
print('壽命:', com1.life)
# 4.刪除屬性:使用壽命
del com1.life
print(com1.__dict__)
# b
# 1.獲取品牌名
name = getattr(com1, 'brand')
print(name)
# 2.修改成綠色
setattr(com1, 'color', '綠')
print(getattr(com1, 'color'))
# 3.添加新屬性:使用壽命
setattr(com1, 'life', 10)
print(getattr(com1, 'life'))
# 4.刪除屬性:使用壽命
delattr(com1, 'life')
print(com1.__dict__)
2.聲明一個(gè)人的類和狗的類:
狗的屬性:名字杯道、顏色、年齡
狗的方法:叫喚
人的屬性:名字责蝠、年齡党巾、狗
人的方法:遛狗
a.創(chuàng)建人的對(duì)象小明,讓他擁有一條狗大黃霜医,然后讓小明去遛大黃
class Dog:
def __init__(self, dog_name, color, age):
self.name = dog_name
self.color = color
self.age = age
def call_out(self):
print('汪汪汪')
class Person:
def __init__(self, p_name, age, dog):
self.name = p_name
self.age = age
self.dog = dog
def walk_dog(self):
print('{}正在遛{}'.format(self.name, self.dog))
p1 = Person('小明', 18, '大黃')
p1.walk_dog()
3.聲明一個(gè)圓類齿拂,自己確定有哪些屬性和方法
class Circular:
def __init__(self, radius):
self.radius = radius
def get_area(self):
return '%.2f' % (3.14*self.radius**2)
def get_peri(self):
return '%.2f' % (6.28*self.radius)
4.創(chuàng)建一個(gè)學(xué)生類:
屬性:姓名,年齡肴敛,學(xué)號(hào)
方法:答到署海,展示學(xué)生信息
創(chuàng)建一個(gè)班級(jí)類:
屬性:學(xué)生,班級(jí)名
方法:添加學(xué)生医男,刪除學(xué)生砸狞,點(diǎn)名, 求班上學(xué)生的平均年齡
class Student:
num = 1
def __init__(self, s_name, age):
self.name = s_name
self.age = age
self.sno = str(self.num).zfill(4)
self.__class__.num += 1
def answer(self, s_name):
if self.name == s_name:
print('到')
def show_student_info(self):
print('姓名:'+self.name, '年齡:', self.age, '學(xué)號(hào):'+self.sno)
s1 = Student('魯班', 5)
s2 = Student('娜可露露', 25)
s3 = Student('貂蟬', 20)
# 答到
s1.answer('魯班')
# 展示學(xué)生信息
s3.show_student_info()
class Class:
sum = 0
def __init__(self, students: list, cname):
self.stu_dict = {}
self.students = []
self.students = students
self.cname = cname
def add_stu(self, s_name, sage):
self.stu_dict['姓名'] = s_name
self.stu_dict['年齡'] = sage
self.stu_dict['學(xué)號(hào)'] = str(int(self.students[-1]['學(xué)號(hào)'])+1).zfill(4)
self.students.append(self.stu_dict)
def delete_stu(self, sno):
for stu_dict in self.students:
if sno == stu_dict['學(xué)號(hào)']:
self.students.remove(stu_dict)
break
def roll_call(self):
for stu_dict in self.students:
print(stu_dict['姓名'])
def avg_age(self):
self.sum = 0
for stu_dict in self.students:
self.sum += stu_dict['年齡']
return '平均年齡:'+str(self.sum/len(self.students))
class1 = Class([{'姓名': '小喬', '年齡': 17, '學(xué)號(hào)': '0001'},
{'姓名': '王昭君', '年齡': 20, '學(xué)號(hào)': '0002'}], 'python1903')
# 添加學(xué)生:小紅
class1.add_stu('小紅', 10)
# 刪除學(xué)生:'0003'
class1.delete_stu('0003')
# 點(diǎn)名
class1.roll_call()
# 求班上學(xué)生的平均年齡
print(class1.avg_age())