1.聲明一個(gè)電腦類: 屬性:品牌旦部、顏色祈搜、內(nèi)存大小 方法:打游戲较店、寫(xiě)代碼、看視頻
class Computer:
def __init__(self, brand, color, memsize):
self.brand = brand
self.color = color
self.memsize = memsize
# a.創(chuàng)建電腦類的對(duì)象容燕,然后通過(guò)對(duì)象點(diǎn)的?方式獲取梁呈、修改、添加和刪除它的屬性
c1 = Computer('apple', '銀色', '6G')
print(c1.brand)
c1.brand = '華碩'
print(c1.brand)
c1.screen_size = '15寸'
print(c1.screen_size)
del c1.color
# b.通過(guò)attr相關(guān)?方法去獲取蘸秘、修改官卡、添加和刪除它的屬性
c2 = Computer('apple', '銀色', '6G')
print(getattr(c2, 'brand'))
setattr(c2, 'color', '白色')
print(c2.color)
setattr(c2, 'screen_size', '16寸')
print(c2.screen_size)
delattr(c2, 'brand')
2.聲明一個(gè)人的類和狗的類:
狗的屬性:名字、顏色醋虏、年齡
狗的方法:叫喚
人的屬性:名字寻咒、年齡、狗
人的方法:遛狗
a.創(chuàng)建人的對(duì)象小明颈嚼,讓他擁有一條狗大黃毛秘,然后讓小明去遛大黃
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def dog_call(self):
print('%s在叫喚' % self.name)
d1 = Dog('大黃', '黃色', '12')
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
self.dog = None
def walk_dog(self):
if not self.dog:
print('沒(méi)有狗')
else:
print('%s正在遛%s' % (self.name, self.dog.name))
p1 = Person('小明', '25')
print(d1.name)
p1.dog = d1 # 讓人擁有狗
print(p1.dog)
p1.walk_dog()
3.聲明一個(gè)圓類,自己確定有哪些屬性和方法
class Circle:
pi = 3.14
def __init__(self, radius):
self.radius = radius
def area(self):
return Circle.pi*(self.radius**2)
def perimeter(self):
return 2*Circle.pi*self.radius
c1 = Circle(7)
print(c1.area())
print(c1.perimeter())
4.創(chuàng)建一個(gè)學(xué)生類:
屬性:姓名阻课,年齡熔脂,學(xué)號(hào)
方法:答到,展示學(xué)生信息
創(chuàng)建一個(gè)班級(jí)類:
屬性:學(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
self.is_in_class = random.randint(0, 1)
def responses(self):
print('%s, 到' % self.name)
def show(self):
print('學(xué)生:%s晰骑, 年齡:%s适秩, 學(xué)號(hào):%s' % (self.name, self.age, self.stu_id))
class Class:
def __init__(self, name):
self.all_students = []
self.name = name
# self.count = 0
def func():
num = 1
while True:
yield 'stu'+str(num)
num += 1
self.create_id = func()
def add_student(self):
name = input('姓名:')
age = input('年齡:')
# self.count += 1
# stu_id = 'stu'+str(self.count).rjust(3, '0')
stu_id = self.create_id
stu = Students(name, int(age), stu_id)
self.all_students.append(stu)
print('學(xué)生:%s, 年齡:%s硕舆, 學(xué)號(hào):%s' % (name, age, stu_id))
def show_all_students(self):
for stu in self.all_students:
stu.show()
def delete(self):
del_name = input('輸入要?jiǎng)h除學(xué)生姓名:')
flag = False
for stu in self.all_students[:]:
if stu.name == del_name:
stu.show()
flag = True
value = input('是否刪除(y/n):')
if value == 'y':
self.all_students.remove(stu)
print('刪除成功')
if not flag:
print('沒(méi)有該學(xué)生')
def call_name(self):
for stu in self.all_students:
print(stu.name)
if stu.is_in_class:
stu.responses()
def average(self):
sum1 = 0
for stu in self.all_students:
sum1 += int(stu.age)
print('python1903的平均年齡為%s' % (sum1/len(self.all_students)))
class1 = Class('python1903')
for i in range(3):
class1.add_student()
class1.delete()
class1.call_name()
class1.average()class Students:
def __init__(self, name, age, stu_id):
self.name = name
self.age = age
self.stu_id = stu_id
self.is_in_class = random.randint(0, 1)
def responses(self):
print('%s, 到' % self.name)
def show(self):
print('學(xué)生:%s秽荞, 年齡:%s, 學(xué)號(hào):%s' % (self.name, self.age, self.stu_id))
class Class:
def __init__(self, name):
self.all_students = []
self.name = name
# self.count = 0
def func():
num = 1
while True:
yield 'stu'+str(num)
num += 1
self.create_id = func()
def add_student(self):
name = input('姓名:')
age = input('年齡:')
# self.count += 1
# stu_id = 'stu'+str(self.count).rjust(3, '0')
stu_id = self.create_id
stu = Students(name, int(age), stu_id)
self.all_students.append(stu)
print('學(xué)生:%s抚官, 年齡:%s扬跋, 學(xué)號(hào):%s' % (name, age, stu_id))
def show_all_students(self):
for stu in self.all_students:
stu.show()
def delete(self):
del_name = input('輸入要?jiǎng)h除學(xué)生姓名:')
flag = False
for stu in self.all_students[:]:
if stu.name == del_name:
stu.show()
flag = True
value = input('是否刪除(y/n):')
if value == 'y':
self.all_students.remove(stu)
print('刪除成功')
if not flag:
print('沒(méi)有該學(xué)生')
def call_name(self):
for stu in self.all_students:
print(stu.name)
if stu.is_in_class:
stu.responses()
def average(self):
sum1 = 0
for stu in self.all_students:
sum1 += int(stu.age)
print('python1903的平均年齡為%s' % (sum1/len(self.all_students)))
class1 = Class('python1903')
for i in range(3):
class1.add_student()
class1.delete()
class1.call_name()
class1.average()