day14 zuoye

1. 聲明一個電腦類

屬性:品牌、顏色、內(nèi)存大小
方法:打游戲、寫代碼、看視頻 def
a.創(chuàng)建電腦類的對象,然后通過對象點的方式獲取、修改、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取、修改、添加和刪除它的屬性

class Computer:
    def __init__(self, brand, color, RAM):
        self.brand = brand
        self.color = color
        self.RAM = RAM
    def playgame(self):
      print(self.brand+'用來打游戲')

    def write_word(self):
        print(self.brand+'用來寫代碼')
    def watch(self):
        print(self.brand+'用來看電視')
# 通過對象點的方式獲取,修改,添加,修改和刪除它的屬性
f1 = Computer('聯(lián)想','白色','8G+256G')
print(f1.brand, f1.color, f1.RAM)
f1.kind = '商務(wù)筆記本'
f1.color = '黑色'
print(f1.brand, f1.color, f1.RAM, f1.kind)
del f1.kind
print(f1.brand, f1.color, f1.RAM)
# 通過attr相關(guān)方法去獲取、修改、添加和刪除它的屬性
f2 = Computer('神州', '黑色', '16G+256G')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),)
setattr(f2, 'RAM', '16G+256G')
print(getattr(f2, 'brand'),getattr(f2,'color'),getattr(f2,'RAM'))
setattr(f2,'kind', '游戲本')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),getattr(f2,'kind'))
delattr(f2, 'kind')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),getattr(f2,'kind','種類不存在'))

f1.playgame()
f1.write_word()
f1.watch()

2.聲明一個人的類和狗的類:

狗的屬性:名字、顏色、年齡
狗的方法:叫喚
人的屬性:名字、 年齡、狗
人的方法:遛狗
a.創(chuàng)建人的對象名字叫小明,讓他擁有一條狗 ,然后讓小明去遛狗

class Dog:
    def __init__(self, name, color, age):
        self.name = name
        self.color = color
        self.age = age
    def dog(self):
        print(self.name+'在叫喚')
class Person:
    def __init__(self, name: str, age: int, dog: Dog):
        self.name = name
        self.age = age
        self.dog = dog
    def walk_the_dog(self):
        self.dog = Dog('小黑', '黑色', 3)
        print(self.name+'在遛'+self.dog.name)
f1 = Person('小明', 18, '小黑')
f1.walk_the_dog()

3.聲明一個矩形類:

屬性: 長、寬
方法:計算周長和面積
a.創(chuàng)建不同的矩形,并且打印其周長和面積

class  Rectangle:
    def __init__(self, long, width):
        self.long = long
        self.width = width
    def count(self):
        sum1 = (self.long+self.width)*2
        area = self.long*self.width
        print('矩形周長是:%s\t矩形面積是:%s' % (sum1,area))
f1 = Rectangle(5,6)
f2 = Rectangle(3,5)
f3 = Rectangle(4,9)
# 面積和州長
f1.count()
f2.count()
f3.count()

4.創(chuàng)建一個學(xué)生類:

屬性:姓名,年齡,學(xué)號,成績
方法:答到,展示學(xué)生信息
創(chuàng)建一個班級類: 屬性:學(xué)生,班級名
方法:添加學(xué)生,刪除學(xué)生,點名, 獲取班級中所有學(xué)生的平均值, 獲取班級中成績最好的學(xué)生

class Students:
    def __init__(self, name, age, stu_id, score):
        self.name = name
        self.age = age
        self.stu_id = stu_id
        self.score = score
    # 達到
    def answer(self):
        print('%s:到!' % self.name)
    # 展示學(xué)生信息
    def show(self):
        print('姓名:%s 年齡:%s  學(xué)號:%s  成績:%s' % (self.name, self.age, self.stu_id, self.score))
class Class:
    def __init__(self,students = [], name=''):
        self.students = students
        self.class_name = name
        # 添加學(xué)生
    def add_stu(self,student = None):
        self.students.append(student)
    # 刪除學(xué)生
    def del_stu(self,student):
        self.students.remove(student)
    # 點名
    def call_name(self,name):
        for stu in self.students:
            print(stu.name)
    # 平均值
    def average(self):
        sum1 = 0
        n = 0
        for stu in self.students:
            sum1 += stu.score
            n +=1
            ave = sum1/n
        return ave
    # 求成績最好的
    def max1(self):
        max1 = self.students[0].score
        name = self.students[0].name
        for stu in self.students:
            if stu.score > max1:
                max1 = stu.score
                name = stu.name
        print(name, max1)

#創(chuàng)建三個學(xué)生
f1 = Students('小明',18,'0001',90)
f2 = Students('小紅',20,'0002',77)
f3 = Students('張三',22,'0003',80)
#將學(xué)生放入班級列表
student = [f1, f2, f3]
#創(chuàng)建一個班級
f4 = Class(student, 'python1807')
print(f1.__dict__)
print(f4)
#增加學(xué)生4李四
student1 = Students('李四',25,'0004',74)
f4.add_stu(student1)
print(student1.__dict__)
#刪除學(xué)生1小明
f4.del_stu(f1)
print(f1.show.__dict__)
#點名
f4.call_name(student)
#求一個班的平均成績
print(f4.average())
#求一個班成績最好的學(xué)生
f4.max1()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容