2019-05-05_day14_面向?qū)ο蠡A(chǔ)_代碼作業(yè)

1

1.聲明?個(gè)電腦類: 屬性:品牌、顏?、內(nèi)存?小 方法:打游戲、寫(xiě)代碼、看視頻
a.創(chuàng)建電腦類的對(duì)象,然后通過(guò)對(duì)象點(diǎn)的?方式獲取、修改、添加和刪除它的屬性
b.通過(guò)attr相關(guān)?方法去獲取、修改、添加和刪除它的屬性

class Computer:
    def __init__(self, brand:str, color:str, memory = 0):
        self.brand = brand
        self.color = color
        self.memory = memory
    def play_games(self):
        print("The feature of playing game")
    def coding(self):
        print("The feature of coding")
    def video(self):
        print("The feature of watching video")

com1 = Computer("apple","white",memory=4096)
print(com1.__dict__)
print(com1.brand)#查
com1.memory = 2048#改
print(com1.memory)
com1.price = 12499#增
print(com1.price)
del com1.color#刪除
print(com1.__dict__)
com2 = Computer("Thinkpad","black",memory=8192)
print(com2.__dict__)
print(getattr(com2,"brand","None"))#查
setattr(com2,"memory",4096)#改
print(getattr(com2,"memory","None"))
setattr(com2,"price",7899)#增
print(getattr(com2,"price","None"))
delattr(com2,"color")#刪除
print(com2.__dict__)

{'brand': 'apple', 'color': 'white', 'memory': 4096}
apple
2048
12499
{'brand': 'apple', 'memory': 2048, 'price': 12499}
{'brand': 'Thinkpad', 'color': 'black', 'memory': 8192}
Thinkpad
4096
7899
{'brand': 'Thinkpad', 'memory': 4096, 'price': 7899}


2

聲明?個(gè)人的類和狗的類:
狗的屬性:名字、顏色、年齡
狗的方法:叫喚
人的屬性:名字、年齡、狗
人的方法:遛狗
a.創(chuàng)建人的對(duì)象小明,讓他擁有一條狗大黃,然后讓小明去遛大黃

class Dog:
    def __init__(self, name:str, color:str, age = 0):
        self.name = name
        self.color = color
        self.age = age
    def bellowing(self):
        print("wang...wang...wang...")
class Person:
    def __init__(self, name:str, dog:str, age = 0):
        self.name = name
        self.age = age
        self.dog = dog
    def take_the_dog(self):
        print("%s遛%s"%(self.name,self.dog))

dog1 = Dog("大黃","yellow",age = 6)
print(dog1.__dict__)
dog1.bellowing()
boy = Person("小明","大黃", age = 18)
print(boy.__dict__)
boy.take_the_dog()

{'name': '大黃', 'color': 'yellow', 'age': 6}
wang...wang...wang...
{'name': '小明', 'age': 18, 'dog': '大黃'}
小明遛大黃

3

聲明?一個(gè)圓類,自己確定有哪些屬性和方法

import math
class Circle:
    def __init__(self,radius):
        self.radius = radius
    def perimeter(self):
        return 2*(self.radius)*math.pi
    def area(self):
        return (self.radius)**2 * math.pi

cir1 = Circle(3)
print("area:%.2f"%cir1.area())
print("perimeter:%.2f"%cir1.perimeter())

area:28.27
perimeter:18.85

4

4.創(chuàng)建?一個(gè)學(xué)?生類:
屬性:姓名,年齡,學(xué)號(hào)
方法:答到,展示學(xué)?生信息

class Student:
    def __init__(self, name:str, age:int, id:str, Flag = True):
        self.name = name
        self.age = age
        self.id = id
        self.Flag = Flag
    def show_message(self):
        message = self.__dict__
        del message["Flag"] #打印除了簽到標(biāo)志的其他信息
        print(message)
    def answer(self):
        if self.Flag:
            print("到")
        else:
            print("他沒(méi)來(lái)")
stu1 = Student("xiaoming", 18, "python1902001", False)
stu1.answer()
stu1.show_message()

他沒(méi)來(lái)
{'name': 'xiaoming', 'age': 18, 'id': 'python1902001'}

5

創(chuàng)建一個(gè)班級(jí)類:
屬性:學(xué)生,班級(jí)名
方法:添加學(xué)生,刪除學(xué)生,點(diǎn)名, 求班上學(xué)生的平均年齡

#[["name",age,Flag]["name1",age1,Flag1]] 數(shù)據(jù)結(jié)構(gòu)二維數(shù)組
class Class:
    def __init__(self, class_name:str, stu:list):
        self.class_name = class_name
        self.stu = stu
    def append_stu(self, stu_name:list):
        self.stu.append(stu_name)
    def remove_stu(self,stu_name:str):
        for i in range(len(self.stu)):
            if stu_name == self.stu[i][0]:
                del self.stu[i]
    def answer(self,stu_name:str):
        for i in range(len(self.stu)):
            if stu_name == self.stu[i][0]:
                if self.stu[i][2]:
                    print("%s到了"%self.stu[i][0])
                else:
                    print("%s沒(méi)來(lái)" % self.stu[i][0])
    def ave(self):
        res = 0
        for i in range(len(self.stu)):
            res += self.stu[i][1]
        res = res / len(self.stu)
        print("平均年齡:",res)

cla1 = Class("python1902",[])
cla1.append_stu(["xiaoming", 18, True])
cla1.append_stu(["xiaohua", 19, False])
print(cla1.__dict__)
cla1.ave()
cla1.answer("xiaoming")
cla1.answer("xiaohua")
cla1.remove_stu("xiaohua")
print(cla1.__dict__)

{'class_name': 'python1902', 'stu': [['xiaoming', 18, True], ['xiaohua', 19, False]]}
平均年齡: 18.5
xiaoming到了
xiaohua沒(méi)來(lái)
{'class_name': 'python1902', 'stu': [['xiaoming', 18, True]]}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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