2018-09-06-day14面向?qū)ο笞鳂I(yè)

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

class Computer:
    def __init__(self):
        self.brand = ''
        self.color = ''
        self.memory = ''

    def play_game(self):
        print('打游戲')

    def write_code(self):
        print('寫代碼')

    def watch_videos(self):
        print('看視頻')

if __name__=='__main__':
    c = Computer()
    c.brand = 'Dell'
    c.color = 'white'
    c.memory = '4GB'
    print(c.brand,c.color,c.memory)
    del c.memory
    try:
        c.price = '4300¥'
        c.color = 'gray'
        print(c.brand,c.color,c.memory)
    except:
        print('沒有那個屬性')
        print(c.brand,c.color,c.price)


    #修改
    setattr(c,'brand','lenovo')
    print(getattr(c,'brand'))

    c.__setattr__('color','blue')
    print(c.__getattribute__('color'))
    #添加
    setattr(c,'time','2018.04')
    print(getattr(c,'time'))

    #刪除
    delattr(c,'brand')
    try:
        print(c.brand)
    except:
        print('品牌已被刪除')

    c.__delattr__('time')
    try:
        print(c.time)
    except:
        print('生產(chǎn)日期已經(jīng)被刪除')
'''
結(jié)果:
Dell white 4GB
沒有那個屬性
Dell gray 4300¥
lenovo
blue
2018.04
品牌已被刪除
生產(chǎn)日期已經(jīng)被刪除
'''

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

class Dog:
    def __init__(self,name,color,age):
        self.name=name
        self.color=color
        self.age=age

    def bark(self):
        print('%s在汪汪叫'%self.name)

class Person:
    def __init__(self,name,age,dog):
        self.name = name
        self.age = age
        self.dog = dog

    def walk_a_dog(self):
      if not self.dog:
        print(‘沒有狗’)
      else:
        print('%s在遛%s'%(self.name,self.dog.name))
if __name__=='__main__':
    d1 = Dog('大黃','黃色','3歲')
    p1 = Person('小明','20歲',d1)

    p1.walk_a_dog()
'''
結(jié)果:
小明在遛大黃
'''

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

class Rect:
    def __init__(self,length,width):
        self.length=length
        self.width=width

    def perimeter(self):
        return (self.length+self.width)*2

    def area(self):
        return self.length*self.width
if __name__=='__main__':
    r1 = Rect(10,20)
    print('周長:%d'%r1.perimeter())
    print('面積:%d'%r1.area())

    r2 = Rect(5,5)
    print('周長:%d'%r2.perimeter())
    print('面積:%d'%r2.area())
'''
結(jié)果:
周長:60
面積:200
周長:20
面積:25
'''

4.創(chuàng)建一個學(xué)生類:
屬性:姓名,年齡,學(xué)號 方法:答到,展示學(xué)生信息 創(chuàng)建一個班級類: 屬性:學(xué)生,班級名 方法:添加學(xué)生 ,刪除學(xué)生,點(diǎn)名

class Student:
    def __init__(self,name,age,stuid):
        self.name=name
        self.age=age
        self.stuid=stuid

    def answer_to(self):
        print('%s到了'%self.name)

    def print_info(self):
        print('我的名字是%s,年齡是%s,學(xué)號是%s,'%(self.name,self.age,self.stuid))

class Class:
    def __init__(self,classname):
        self.student_list=[]
        self.classname=classname

    def add_student(self,student):
        self.student_list.append(student)

    def del_student(self,student):
        if self.student_list:
            self.student_list.remove(student)

    def naming(self):
        if self.student_list:
            for student in self.student_list:
                print(student.name)
                student.answer_to()

if __name__ == "__main__":
    s1 = Student("張三",'20','001')
    s2 = Student("李四",'22','002')
    s3 = Student("王五",'21','003')
    s4 = Student("張流",'23','004')
    s5 = Student("李思",'19','005')

    c1 = Class('1班')
    c1.student_list=[s1,s2,s3,s4,s5]
    print('------------------------------------')
    c1.naming()


    c1.add_student(Student('趙六','18','006'))
    print('------------------------------------')
    c1.naming()

    c1.del_student(s1)
    print('------------------------------------')
    c1.naming()
'''
結(jié)果:
------------------------------------
張三
張三到了
李四
李四到了
王五
王五到了
張流
張流到了
李思
李思到了
------------------------------------
張三
張三到了
李四
李四到了
王五
王五到了
張流
張流到了
李思
李思到了
趙六
趙六到了
------------------------------------
李四
李四到了
王五
王五到了
張流
張流到了
李思
李思到了
趙六
趙六到了
'''
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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