1.聲明?個(gè)電腦類(lèi): 屬性:品牌辰狡、顏?、內(nèi)存?小 方法:打游戲遇绞、寫(xiě)代碼堵第、看視頻
a.創(chuàng)建電腦類(lèi)的對(duì)象,然后通過(guò)對(duì)象點(diǎn)的?方式獲取易遣、修改、添加和刪除它的屬性
b.通過(guò)attr相關(guān)?方法去獲取、修改椎工、添加和刪除它的屬性
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print('打游戲')
def coding(self):
return '寫(xiě)代碼'
def watch_video(self):
return '看視頻'
computer1 = Computer('Apple', 'black', '8G')
# 方法1
# 獲取屬性
print(computer1.brand)
print(computer1.color)
print(computer1.memory)
# 修改屬性
computer1.brand = 'new Apple'
computer1.color = 'new white'
computer1.memory = 'new 8G'
# 添加屬性
computer1.price = '$1000'
print(computer1.price)
# 刪除屬性
del computer1.price
# print(computer1.price) AttributeError: 'Computer' object has no attribute 'price'
# 方法2
# 獲取屬性
print(getattr(computer1, 'brand'))
print(getattr(computer1, 'color'))
print(getattr(computer1, 'memory'))
# 修改屬性
setattr(computer1, 'brand', 'new new Apple')
print(computer1.brand)
setattr(computer1, 'color', 'new new white')
print(computer1.color)
setattr(computer1, 'memory', 'new new 8G')
print(computer1.memory)
# 添加屬性
setattr(computer1, 'price', '$200')
print(computer1.price)
# 刪除屬性
delattr(computer1, 'price')
print(computer1.price) # AttributeError: 'Computer' object has no attribute 'price'
2.聲明?個(gè)人的類(lèi)和狗的類(lèi):
狗的屬性:名字、顏?色蜀踏、年年齡
狗的?方法:叫喚
人的屬性:名字维蒙、年年齡、狗
人的?方法:遛狗
a.創(chuàng)建?人的對(duì)象?小明果覆,讓他擁有?一條狗?大?黃颅痊,然后讓?小明去遛?大?黃
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def method(self):
print('汪汪汪')
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
self.dog = None
def personMethod(self):
if not self.dog:
print('沒(méi)狗')
else:
print('%s溜%s' % (self.name,self.dog.name))
bigHuang = Dog('大黃', '黃色', 2)
person1 = Person('小明', 18)
person1.dog = bigHuang
person1.personMethod()
3.聲明?一個(gè)圓類(lèi),自己確定有哪些屬性和方法
import math
class Circle:
pi = math.pi
def __init__(self, radius):
self.radius = radius
def area(self):
return '面積為%.2f' % (Circle.pi*self.radius**2)
def perimeter(self):
return '周長(zhǎng)為%.2f' % (2*Circle.pi*self.radius)
circle1 = Circle(3)
print(circle1.area())
print(circle1.perimeter())
4.創(chuàng)建?一個(gè)學(xué)?生類(lèi):
屬性:姓名局待,年齡斑响,學(xué)號(hào)
方法:答到,展示學(xué)?生信息
創(chuàng)建?一個(gè)班級(jí)類(lèi):
屬性:學(xué)?生钳榨,班級(jí)名
方法:添加學(xué)?生舰罚,刪除學(xué)生,點(diǎn)名, 求班上學(xué)生的平均年齡
class Students:
def __init__(self, name='', age='', stu_id=''):
self.name = name
self.age = age
self.stu_id = stu_id
def answer(self):
return "到"
def show_info(self):
return {"姓名":self.name, "年齡":self.age, "學(xué)號(hào)":self.stu_id}
stu1 = Students("小明","22","022")
print(stu1.answer())
print(stu1.show_info())
class ClassGrade:
def __init__(self, stus=[], class_name=''):
self.stus = stus
self.class_name = class_name
def add_stu(self, stu_name='', stu_age=0):
stu = {}
stu["姓名"]=stu_name
stu["年齡"]=stu_age
self.stus.append(stu)
return self.stus
def del_stu(self, stu_name):
for stu in self.stus:
if stu["姓名"]==stu_name:
self.stus.remove(stu)
break
#
def check_name(self, stu_name):
return stu_name+'到'
def aver_age(self):
sum_age = 0
for stu in self.stus:
sum_age += stu["年齡"]
return sum_age/len(self.stus)
def show_info(self):
return self.class_name, self.stus
class1 = ClassGrade(class_name="1903")
#添加學(xué)生
class1.add_stu("小紅", 22)
class1.add_stu("小明", 23)
print(class1.show_info())
#刪除學(xué)生
class1.del_stu("小明")
print("刪除學(xué)生后的班級(jí)信息:", class1.show_info())
#點(diǎn)名
print(class1.check_name("小紅"))
#求學(xué)生平均年齡
print("該班學(xué)生的平均年齡是", class1.aver_age())