1.聲明?個電腦類: 屬性:品牌、顏?、內(nèi)存?小 方法:打游戲、寫代碼、看視頻
a.創(chuàng)建電腦類的對象,然后通過對象點的?方式獲取、修改、添加和刪除它的屬性
b.通過attr相關(guān)?方法去獲取、修改、添加和刪除它的屬性
import random
class Computer:
"""電腦類"""
def __init__(self, brand, color, ram):
self.brand = brand
self.color = color
self.ram = ram
def paly_game(self): # 有波浪號警告,說明選的方法不對
return '打游戲'
def write_code(self, lang: str):
return '寫%s代碼' % lang
def watch_video(self):
return '看視頻'
c1 = Computer('華為', '白色', '512G')
print(c1.brand) # 華為
c1.color = '黑色'
print(c1.color) # 黑色
c1.price = '1000'
print(c1.price) # 1000
# del c1.price
# print(c1.price) # AttributeError: 'Computer' object has no attribute 'price'
print(getattr(c1, 'brand')) # 華為
setattr(c1, 'color', '紅色')
print(c1.color) # 紅色
setattr(c1, 'price', 200)
print(c1.price) # 200
# delattr(c1, 'price')
# print(c1.price) # AttributeError: 'Computer' object has no attribute 'price'
2.聲明?個人的類和狗的類:
狗的屬性:名字、顏?、年齡
狗的?方法:叫喚
人的屬性:名字、年齡、狗
人的?方法:遛狗
a.創(chuàng)建?的對象小明,讓他擁有一條狗大黃,然后讓小明去遛大黃
class Dog:
"""狗"""
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def shout(self):
print('%s在叫:汪汪汪' % self.name)
class Person:
"""人類"""
def __init__(self, name, age):
self.name = name
self.age = age # 擁有年齡
self.dog = None # 狗類的對象,具體的某只狗
def walk_the_dog(self):
if not self.dog:
print('沒有狗')
else:
print('%s牽著%s在散步' % (self.name, self.dog.name))
p1 = Person('小明', 35) # 創(chuàng)建人的對象 ??沒有聲明self.dog就能用?
dog1 = Dog('大黃', '白色', 5) # 創(chuàng)建狗的對象
p1.dog = dog1 # 讓人擁有狗
p1.walk_the_dog()
3.聲明?圓類,自己確定有哪些屬性和方法
class Circle:
pi = 3.1415926
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * Circle.pi * self.radius
def area(self):
return Circle.pi * self.radius ** 2
c1 = Circle(3)
print(c1.perimeter())
print(c1.area())
4.創(chuàng)建一個學(xué)?生類:
屬性:姓名,年齡,學(xué)號
方法:答到,展示學(xué)?生信息
創(chuàng)建?個班級類:
屬性:學(xué)生,班級名
方法:添加學(xué)?,刪除學(xué)生,點名, 求班上學(xué)生的平均年齡
class Student:
def __init__(self, name, age=0, study_id='000'):
self.name = name
self.age = age
self.study_id = study_id
self.is_in_class = random.randint(0,1)
def respond(self):
print('%s,到!' % self.name)
def show(self):
print('姓名:%s 年齡:%d 學(xué)號:%s') % (self.name, self.age, self.study_id)
class Class:
def __init__(self, class_name):
self.students = [] # 一個班有多個學(xué)生:一個列表,列表中的元素是學(xué)生
self.class_name = class_name
def func():
num = 1
while True:
yield 'stu'+str(num)
num += 1
self.creat_id = func() # 學(xué)號生成器
def add_student(self):
name = input('學(xué)生姓名;')
age = int(input('學(xué)生的年齡:'))
study_id = next(self.creat_id)
stu = Student(name, age, study_id)
self.students.append(stu)
def del_student(self):
"""刪除學(xué)生"""
del_name = input('請輸入要刪除的學(xué)生的名字:')
flag = False
for stu in self.students:
if stu.name == del_name:
flag = True
stu.show()
input('是否刪除(y/n)')
if value == 'y':
self.students.remove(stu)
if not flag:
print('沒有該學(xué)生!')
def call_name(self):
"""點名"""
for stu in self.students:
print(stu.name)
if stu.is_in_class:
stu.respond()
def avg_age(self):
"""求平均年齡"""
ages = 0
for stu in self.students():
ages += stu.age
return ages / len(self.students)
def show_all_students(self):
print('=========%s的學(xué)生============')
for stu in self.students:
stu.show()
c1 = Class('py1903')
c2 = Class('py1904')
c1.add_student() # 001
c1.add_student() # 002
c2.add_student() # 001
c1.add_student() # 003
c1.show_all_students()
c2.show_all_students()