1.聲明?個(gè)電腦類: 屬性:品牌勾笆、顏?阿宅、內(nèi)存?小 方法:打游戲候衍、寫代碼、看視頻
a.創(chuàng)建電腦類的對(duì)象洒放,然后通過(guò)對(duì)象點(diǎn)的?方式獲取蛉鹿、修改、添加和刪除它的屬性
class Computer:
def __init__(self, brand='', color='', ram=''):
self.brand = brand
self.color = color
self.ram = ram
def coding(self):
print('coding~~~')
def wvideo(self):
print('watching')
# 對(duì)象點(diǎn)
c1 = Computer('dell', 'grey', '16')
# 查
print(c1.brand)
# 修改
c1.color = 'pink'
print(c1.color)
# 添加
c1.price = '15000'
print(c1.price)
# 刪除
print(c1.ram)
del c1.ram
print(c1.ram)
b.通過(guò)attr相關(guān)?方法去獲取往湿、修改妖异、添加和刪除它的屬性
# 查
getattr(c1, 'brand')
print(c1.brand)
# 修改
setattr(c1, 'color', 'pink')
print(c1.color)
# 添加
setattr(c1, 'price', '15000')
print(c1.price)
#刪除
delattr(c1, 'ram')
# print(c1.ram)
2.聲明?個(gè)人的類和狗的類:
狗的屬性:名字惋戏、顏色、年齡
狗的方法:叫喚
人的屬性:名字他膳、年年齡响逢、狗
人的方法:遛狗
a.創(chuàng)建?人的對(duì)象小明,讓他擁有?一條狗大黃棕孙,然后讓小明去遛大黃
class Dog:
def __init__(self, name='', color='', age=''):
self.name = name
self.color = color
self.age = age
def bark(self):
print('嗷嗚~~~')
d1 = Dog(name='大黃')
class People:
def __init__(self, name='', age='', pet=''):
self.name = name
self.age = age
self.pet = pet
def walk_dog(self):
print('%s溜%s' % (p1.name, p1.pet))
p1 = People(name='小明', pet='大黃')
p1.walk_dog()
3.聲明?一個(gè)圓類舔亭,自己確定有哪些屬性和方法
pi = 3.1415926
class Circle:
def __init__(self, radius = 0):
self.radius = radius
def area(self):
print(pi * self.radius ** 2)
def perimeter(self):
print(2*pi*self.radius)
c1 = Circle(5)
c1.area()
c1.perimeter()
4.創(chuàng)建?一個(gè)學(xué)?生類:
屬性:姓名,年齡蟀俊,學(xué)號(hào)
方法:答到钦铺,展示學(xué)?生信息
創(chuàng)建?一個(gè)班級(jí)類:
屬性:學(xué)?生,班級(jí)名
方法:添加學(xué)?生肢预,刪除學(xué)生矛洞,點(diǎn)名, 求班上學(xué)生的平均年齡
import random
class Student:
def __init__(self, name, age=0, num='0001'):
self.name = name
self.age = age
self.num = num
self.state = random.randint(0, 1) # 是否在教室的狀態(tài), 0 -> 不在, 1 -> 在
def amount(self):
print('%s 到!' % self.name)
def show(self):
print('姓名:%s 年齡:%d 學(xué)號(hào):%s' % (self.name, self.age, self.num))
class Class:
def __init__(self, name):
self.students = [
Student('stu1', 19, '001'),
Student('stu2', 20, '002'),
Student('stu3', 23, '003'),
Student('stu1', 19, '004'),
Student('stu4', 30, '005'),
]
self.name = name
# 學(xué)號(hào)生成器
self.__nums = ('stu'+str(num).zfill(3) for num in range(50))
# 添加學(xué)?生
def add_student(self):
# 輸入學(xué)生信息
stu_name = input('姓名:')
stu_age = int(input('年齡:'))
stu_num = next(self.__nums)
# 創(chuàng)建學(xué)生對(duì)象
stu = Student(stu_name, stu_age, stu_num)
# 將學(xué)生添加到班級(jí)中
self.students.append(stu)
# 刪除學(xué)生
def del_student(self):
del_name = input('刪除學(xué)生的姓名:')
for stu in self.students[:]:
if stu.name == del_name:
self.students.remove(stu)
# 點(diǎn)名
def call_the_roll(self):
for stu in self.students:
# 喊名字
print(stu.name)
# 如果在教室
if stu.state:
# 答到
stu.amount()
else:
print('....')
def average_age(self):
sum1 = 0
for stu in self.students:
sum1 += stu.age
average = sum1//len(self.students)
print('平均年齡: %d' % average)
return average
def cl_show(self):
for stu in self.students:
stu.show()
class1 = Class('py1902')
print('=========添加學(xué)生===========')
# for _ in range(5):
class1.add_student()
class1.cl_show()
print('=========刪除學(xué)生===========')
class1.del_student()
class1.cl_show()
print('=========點(diǎn)名===========')
class1.call_the_roll()
class1.average_age()