1.聲明?個(gè)電腦類: 屬性:品牌、顏?柒巫、內(nèi)存?小 方法:打游戲励堡、寫代碼、看視頻
class Computer:
def __init__(self, brand, color, ram):
self.brand = brand
self.color = color
self.ram = ram
@staticmethod
def play_game():
print('這是在玩游戲')
@staticmethod
def write_code():
print('這是在寫代碼')
@staticmethod
def see_video():
print('這是在看視頻')
a.創(chuàng)建電腦類的對(duì)象堡掏,然后通過(guò)對(duì)象點(diǎn)的?方式獲取应结、修改、添加和刪除它的屬性
puter = Computer('聯(lián)想', '黑色', '8G')
# 獲取品牌屬性
print(puter.brand)
# 獲取顏色屬性
print(puter.color)
# 獲取內(nèi)存信息
print(puter.ram)
print(puter.__dict__)
# 修改品牌屬性
puter.brand = '戴爾'
# 修改顏色屬性
puter.color = '藍(lán)色'
# 修改內(nèi)存信息
puter.ram = '4G'
print(puter.__dict__)
# 添加屬性
puter.size = '14寸'
print(puter.__dict__)
# 刪除屬性
del puter.size
print(puter.__dict__)
這是結(jié)果:
E:\LHYPython\homework\day15_類和對(duì)象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_類和對(duì)象/01_work.py
聯(lián)想
黑色
8G
{'brand': '聯(lián)想', 'color': '黑色', 'ram': '8G'}
{'brand': '戴爾', 'color': '藍(lán)色', 'ram': '4G'}
{'brand': '戴爾', 'color': '藍(lán)色', 'ram': '4G', 'size': '14寸'}
{'brand': '戴爾', 'color': '藍(lán)色', 'ram': '4G'}
Process finished with exit code 0
b.通過(guò)attr相關(guān)?方法去獲取泉唁、修改鹅龄、添加和刪除它的屬性
puter2 = Computer('蘋果', '銀色', '8G')
# 獲取品牌屬性
print(getattr(puter2, 'brand', '未知'))
# 獲取顏色屬性
print(getattr(puter2, 'color', '未知'))
# 獲取內(nèi)存屬性
print(getattr(puter2, 'ram', '未知'))
print(puter2.__dict__)
# 修改品牌屬性
setattr(puter2, 'brand', '外星人')
# 修改顏色屬性
setattr(puter2, 'color', '黑色')
# 修改內(nèi)存屬性
setattr(puter2, 'ram', '12G')
print(puter2.__dict__)
# 添加尺寸屬性
setattr(puter2, 'size', '15.6寸')
print(puter2.__dict__)
# 刪除尺寸屬性
delattr(puter2, 'size')
print(puter2.__dict__)
這是結(jié)果:
E:\LHYPython\homework\day15_類和對(duì)象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_類和對(duì)象/01_work.py
蘋果
銀色
8G
{'brand': '蘋果', 'color': '銀色', 'ram': '8G'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G', 'size': '15.6寸'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G'}
Process finished with exit code 0
2.聲明?個(gè)人的類和狗的類:
狗的屬性:名字、色亭畜、年齡
狗的?方法:叫喚
人的屬性:名字扮休、年齡、狗
人的方法:遛狗
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
print('{}正在遛著{},'.format(self.name, self.dog), end='')
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def bark(self):
print('這個(gè)時(shí)候{}卻突然叫喚起來(lái)'.format(self.name))
a.創(chuàng)建人的對(duì)象小明拴鸵,讓他擁有?一條狗?黃玷坠,然后讓小明去遛?黃
dog = Dog('大黃', '黃色', 3)
person = Person('小明', 24, dog.name)
person.walk_dog()
dog.bark()
E:\LHYPython\homework\day15_類和對(duì)象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_類和對(duì)象/02_work.py
小明正在遛著大黃,這個(gè)時(shí)候大黃卻突然叫喚起來(lái)
Process finished with exit code 0
3.聲明一個(gè)圓類:
class Circle:
def __init__(self, r):
self.pi = pi
self.r = r
def area(self):
print('r={}的圓面積是{}'.format(self.r, pi * self.r ** 2))
def perim(self):
print('r={}的圓周長(zhǎng)是{}'.format(self.r, pi * self.r * 2))
這是結(jié)果:
E:\LHYPython\homework\day15_類和對(duì)象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_類和對(duì)象/03_work.py
r=10的圓面積是314.1592653589793
r=10的圓周長(zhǎng)是62.83185307179586
Process finished with exit code 0
4.創(chuàng)建一個(gè)學(xué)?生類:
屬性:姓名,年齡劲藐,學(xué)號(hào)
方法:答到八堡,展示學(xué)生信息
class Student:
def __init__(self, ids, name, age):
self.id = ids
self.name = name
self.age = age
def check_in(self):
print('學(xué)生{}已到'.format(self.name))
def show_info(self):
info = list(dict(self.__dict__).values())
print('學(xué)號(hào):{}, 姓名:{}, 年齡{}'.format(info[0], info[1], info[2]))
這是結(jié)果:
E:\LHYPython\homework\day15_類和對(duì)象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_類和對(duì)象/04_work.py
學(xué)生小明已到
學(xué)號(hào):stu001, 姓名:小明, 年齡20
Process finished with exit code 0
創(chuàng)建一個(gè)班級(jí)類:
屬性:學(xué)生,班級(jí)名
方法:添加學(xué)生聘芜,刪除學(xué)生兄渺,點(diǎn)名, 求班上學(xué)生的平均年齡
class ClassGrade:
def __init__(self, student):
self.student = student
self.grade_name = 1
def add_stu(self, num):
while True:
num += 1
stu_id = 'stu' + str(num).rjust(3, '0')
stu_name = input('name:')
stu_age = int(input('age:'))
self.student.append({'id': stu_id, 'name': stu_name, 'age': stu_age})
setattr(self, 'student', self.student)
print('添加成功')
input_value = input('是否繼續(xù)添加(Y/N)')
if input_value != 'Y' and input_value != 'y':
break
return
def del_stu(self, item_num):
del self.student[item_num - 1]
setattr(self, 'student', self.student)
print('刪除成功')
print(self.student)
def aver_age(self):
sum_age = 0
for item in self.student:
sum_age += item['age']
aver_age = sum_age / len(self.student)
print('平均年齡為{}'.format(aver_age))
grade = ClassGrade(student=[])
num = 0
while True:
print(fileManage.read_text_file('page.txt'))
input_value = input('請(qǐng)選擇你的操作')
if input_value == '1':
grade.add_stu(num)
elif input_value == '2':
item_num = 0
for item in grade.student:
item_num += 1
print(str(item_num) + '> ' + '姓名:{}, 學(xué)號(hào):{}, 年齡{}'
.format(item['name'], item['id'], item['age']))
grade.del_stu(item_num)
elif input_value == '3':
input_name = input('點(diǎn)名:')
for item in grade.student:
if item['name'] == input_name:
student = Student(item['id'], item['name'], item['age'])
student.check_in()
student.show_info()
elif input_value == '4':
grade.aver_age()
elif input_value == 'q':
exit()