1.聲明?個(gè)電腦類: 屬性:品牌敛腌、顏?、內(nèi)存?小 方法:打游戲惫皱、寫代碼像樊、看視頻 。
a.創(chuàng)建電腦類的對(duì)象旅敷,然后通過對(duì)象.的方式獲取生棍、修改、添加和刪除它的屬性
b.通過attr相關(guān)?方法去獲取媳谁、修改涂滴、添加和刪除它的屬性
class Computer:
def __init__(self, brand1, color1,memorySize1):
self.brand = brand1
self.color = color1
self.memorySize = memorySize1
def playing_games(self):
print('%s電腦適合打游戲' % self.brand)
def writing_codes(self):
print('%s電腦適合寫代碼' % self.brand)
def watching_videos(self):
print('%s電腦適合看視頻' % self.brand)
# a.創(chuàng)建電腦類的對(duì)象,然后通過對(duì)象.的方式獲取晴音、修改柔纵、添加和刪除它的屬性
computer1 = Computer('ThinkPad','黑色','8g')
print(computer1.brand) # 查
computer1.brand = '外星人' # 改
computer1.playing_games()
computer1.price = '10000元' # 增
print(computer1.price)
del computer1.price # 刪
# print(computer1.price) AttributeError: 'Computer' object has no attribute 'price'
# b.通過attr相關(guān)?方法去獲取、修改锤躁、添加和刪除它的屬性
computer2 = Computer('Apple', '銀色', '16g')
computer2.writing_codes()
print(getattr(computer2, 'color')) # 查
print(getattr(computer2, 'price', '無(wú)價(jià)格'))
setattr(computer2, 'color','金色') # 改
print(computer2.color)
setattr(computer2, 'price','8000元') # 增
print(computer2.price)
delattr(computer2,'price')
# print(computer2.price) AttributeError: 'Computer' object has no attribute 'price'
2.聲明?個(gè)人的類和狗的類:
狗的屬性:名字首量、顏色、年齡
狗的方法:叫喚
人的屬性:名字进苍、年齡加缘、狗
人的?方法:遛狗
a.創(chuàng)建人的對(duì)象小明,讓他擁有一條狗大黃觉啊,然后讓小明去遛大黃
class Human:
def __init__(self,name1,age1,dog1):
self.name = name1
self.age = age1
self.dog = dog1
def walk_dog(self):
print('%s牽著%s在公園散步' % (self.name, self.dog))
class Dog:
def __init__(self, name1,color1,age1):
self.name = name1
self.color = color1
self.age = age1
def bark(self):
print('%s在亂叫' % self.name)
human1 = Human('小明', '18', '大黃')
dog1 = Dog('大黃','黃色', '3')
human1.walk_dog()
3.聲明?一個(gè)圓類拣宏,自己確定有哪些屬性和方法
import math
class Circle:
# 定義屬性:半徑和圓心角
def __init__(self,radius, angle):
self.radius = radius
self.angle = angle
# 函數(shù):求圓的周長(zhǎng)
def perimeter(self):
return round(2 * math.pi * self.radius,2)
# 函數(shù):求圓的面積
def area(self):
return round(math.pi * self.radius ** 2,2)
# 函數(shù):求弧長(zhǎng)
def length(self):
return round(math.pi * self.angle * self.radius / 180,2)
circle1 = Circle(10, 360)
print(circle1.perimeter())
print(circle1.area())
print(circle1.length())
4.創(chuàng)建一個(gè)學(xué)生類:
屬性:姓名,年齡杠人,學(xué)號(hào)
方法:答到勋乾,展示學(xué)?生信息
創(chuàng)建一個(gè)班級(jí)類:
屬性:學(xué)生宋下,班級(jí)名
方法:添加學(xué)生,刪除學(xué)生辑莫,點(diǎn)名, 求班上學(xué)生的平均年齡
import json
class Students:
def __init__(self, name, age=0, stu_id=1):
self.name = name
self.age = age
self.stu_id = stu_id
# 答到
def achieve(self):
print('%s到' % self.name)
# 獲取學(xué)生信息学歧,生成字典
def information(self):
stu_dict = {'姓名':self.name, '年齡':self.age, '學(xué)號(hào)':self.stu_id}
return stu_dict
class Class_stu:
def __init__(self, students,class_name):
self.students = students
self.class_name = class_name
# 生成器生成學(xué)號(hào),通過列表的長(zhǎng)度來(lái)確定每次執(zhí)行程序時(shí)學(xué)號(hào)的初始值
self.stu_id = ('stu' + str(num).zfill(3) for num in range(len(students)+1,100))
# 添加學(xué)生
def add_stu(self):
while True:
print('1.添加學(xué)生\n2.返回')
value = input('請(qǐng)選擇(1-2)')
if value == '1':
stu_name = input('輸入姓名')
stu_age = input('輸入年齡')
stu_id = next(self.stu_id) # 生成器獲取學(xué)號(hào)
stu = Students(stu_name,stu_age,stu_id).information() # 調(diào)用Students類中的方法來(lái)保存學(xué)生信息
self.students.append(stu) # 將學(xué)生信息添加至列表
else:
break
with open('test.json', 'w',encoding='utf-8') as f:
f.write(json.dumps(self.students)) # 將學(xué)生信息寫入文件
# 刪除學(xué)生
def delete_stu(self):
name1 = input('請(qǐng)輸入要?jiǎng)h除的學(xué)生名字:')
for i in students:
if i['姓名'] == name1:
students.remove(i)
break
else:
print('無(wú)此人')
for j in range(len(students)):
students[j]['學(xué)號(hào)'] = 'stu' + str(j+1).zfill(3) # 重新生成學(xué)號(hào)
with open('test.json', 'w',encoding='utf-8') as f:
f.write(json.dumps(self.students)) # 重新寫入學(xué)生信息
# 點(diǎn)名
def call_stu(self):
name1 = input('請(qǐng)輸入要點(diǎn)的學(xué)生名字:')
for i in students:
if i['姓名'] == name1:
Students(name1).achieve() # 調(diào)用Students類中的方法
break
else:
print('無(wú)此人')
# 求平均年齡
def ave_age(self):
sum1 = 0
for i in students:
sum1 += int(i['年齡'])
aver = round(sum1 / len(students),2)
print('平均年齡為:',aver)
# 讀取主界面
def show_mainpage():
with open('test.txt', encoding='utf-8') as f:
page = f.read()
print(page)
# 主程序
with open('test.json',encoding='utf-8') as f:
students = json.loads(f.read())
c1 = Class_stu(students,'py1902')
while True:
show_mainpage()
value = input('請(qǐng)選擇(1-5)')
if value == '1':
c1.add_stu()
elif value == '2':
c1.delete_stu()
elif value == '3':
c1.call_stu()
elif value == '4':
c1.ave_age()
else:
break