- 聲明一個電腦類
屬性:品牌甜无、顏色、內(nèi)存大小
方法:打游戲哥遮、寫代碼岂丘、看視頻
a.創(chuàng)建電腦類的對象,然后通過對象點的方式獲取眠饮、修改奥帘、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取、修改仪召、添加和刪除它的屬性
class Computer:
def __init__(self, brand, color, size):
self.brand = brand
self.color = color
self.size = size
def play_game(self):
print('打游戲')
def write_code(self):
print('寫代碼')
def see_move(self):
print('看視頻')
computer1 = Computer('華碩', '黑色', "12GB")
# 1.獲取
print("a.品牌:%s 顏色:%s 內(nèi)存大小:%s" % (computer1.brand, computer1.color, computer1.size))
print("b.品牌:%s 顏色:%s 內(nèi)存大小:%s" % (getattr(computer1, "brand"), getattr(computer1, "color"), getattr(computer1, "size")))
# 2.修改
computer1.brand = "外星人"
setattr(computer1, 'color', '白色')
print("品牌:%s, 顏色:%s" % (computer1.brand, computer1.color))
# 3.添加
computer1.disc = '1T'
setattr(computer1, 'digit', '64')
print("磁盤大小:%s 位數(shù):%s" %(computer1.disc, computer1.digit))
# 4.刪除
del computer1.brand
delattr(computer1, 'color')
2.聲明一個人的類和狗的類:
狗的屬性:名字寨蹋、顏色牲距、年齡
狗的 法:叫喚
人的屬性:名字、 年齡钥庇、狗
人的方法:遛狗
a.創(chuàng)建人的對象名字叫小明牍鞠,讓他擁有一條狗 ,然后讓小明去遛狗
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def call(self):
print('汪汪汪')
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def wolk(self):
print('溜%s' % self.dog.name)
dog1 = Dog('二哈', '黑色', '2')
person1 = Person('小明', '20', dog1)
person1.wolk()
3.聲明一個矩形類:
屬性: 長评姨、寬
方法:計算周長和面積
a.創(chuàng)建不同的矩形难述,并且打印其周長和面積
class Rectangle:
def __init__(self, width, length):
self.width = width
self.length = length
def perimeter(self):
per = (int(self.length) + int(self.width)) * 2
print('周長 %s' % per)
def area(self):
ar = int(self.length) * int(self.width)
print('面積 %s' % ar)
rectangle1 = Rectangle(40, 50)
rectangle1.perimeter()
rectangle1.area()
rectangle2 = Rectangle(60, 20)
rectangle2.perimeter()
rectangle2.area()
rectangle3 = Rectangle(20, 40)
rectangle3.perimeter()
rectangle3.area()
4.創(chuàng)建一個學(xué)生類:
屬性:姓名,年齡吐句,學(xué)號胁后,成績
方法:答到,展示學(xué)生信息
創(chuàng)建一個班級類: 屬性:學(xué)生嗦枢,班級名
方法:添加學(xué)生攀芯,刪除學(xué)生,點名, 獲取班級中所有學(xué)生的平均值文虏, 獲取班級中成績最好的學(xué)生
class Student:
def __init__(self, name, age, sno, score):
self.name = name
self.age = age
self.sno = sno
self.score = score
def answer(self):
print('%s 到!' % self.name)
def show_info(self):
print("姓名:%s 年齡:%d 學(xué)號:%s 成績:%s" % (self.name, self.age, self.sno, self.score))
class Class:
def __init__(self, name, student_list: list):
self.name = name
self.student_list = student_list
def add_student(self, student):
for item in self.student_list:
if item.sno == student.sno:
print("學(xué)號已經(jīng)存在")
return
self.student_list.append(student)
def detele_student(self, sno):
for item in self.student_list:
if item.snp == sno:
self.student_list.remove(item)
break
else:
print('該學(xué)生不存在')
def avg(self):
i = 0
for item in self.student_list:
i += item.score
i /= len(self.student_list)
print('分?jǐn)?shù)平均值:', i)
def max(self):
max1 = max(self.student_list, key=lambda item: item.score)
max1.show_info()
def call_student(self, s_name):
for item in self.student_list:
if item.name == s_name:
item.answer()
break
else:
print('沒有此人')
student1 = Student('小明', 20, 'python001', 99)
student2 = Student('小紅', 18, 'python002', 87)
student3 = Student('小花', 22, 'python003', 93)
student_list = [student1, student2, student3]
class1 = Class('python', student_list)
class1.avg()
class1.max()
class1.call_student('小明')
class1.detele_student('python001')
class1.call_student('小明')
class1.call_student('小王')
student4 = Student('小王', 23, 'python004', 83)
class1.add_student(student4)
class1.call_student('小王')