2018-09-06-作業(yè)

1.聲明一個(gè)電腦類: 屬性:品牌、顏色、內(nèi)存 方法:打游戲、寫代碼、看視頻

class Computer:
    def __init__(self,type,color,memery):
        self.type=type
        self.color=color
        self.memery=memery

    @classmethod
    def play_game(cls):
        print('play_game')

    @classmethod
    def write_code(cls):
        print('write_code')

    @classmethod
    def watch_video(cls):
        print('watch_video')

a.創(chuàng)建電腦類的對(duì)象,然后通過對(duì)象點(diǎn)的方式獲取、修改、添加和刪除它的屬性
b.通過attr相關(guān)方法去獲取、修改、添加和刪除它的屬性

a
創(chuàng)建對(duì)象
com1=Computer('聯(lián)想','黑色','512G')
# 獲取
print(com1.type)   #聯(lián)想
# 修改
com1.type='華碩'
print(com1.type)   #華碩
# 添加
com1.size=16
print(com1.size)   #16
# 刪除
del com1.type
# print(com1.type)   # AttributeError: 'Computer' object has no attribute 'type'
b
com2=Computer('聯(lián)想','黑色','512G')
# 獲取
print(getattr(com2,'type'))   #聯(lián)想
# 修改
setattr(com2,'type','華碩')
print(getattr(com2,'type'))   #華碩
# 添加
setattr(com2,'size',16)
print(getattr(com2,'size'))   #16
# 刪除
delattr(com2,'type')
# print(com2.type)   # AttributeError: 'Computer' object has no attribute 'type'

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

class Person:
    def __init__(self,name,age,dog):
        self.name = name
        self.age = age
        self.dog = dog
    def walk_the_dog(self):
        print('正在遛狗中')

class Dog:
    def __init__(self,name,age=5,color='黃色'):
        self.name = name
        self.age = age
        self.color = color
    def bark(self):
        print('汪,汪,汪')
p1=Person('小明',18,'大黃')
dog1=Dog('大黃')
p1.walk_the_dog()
dog1.bark()

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

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

    def perimeter(self):
        perimeter=self.length*2+self.width*2
        print('周長(zhǎng)是%d'%(perimeter))
        return perimeter

    def area(self):
        area=self.length*self.width
        print('面積是%d'%(area))
        return area

r1=Rectangle(30,20)
r1.perimeter()
r1.area()
r2=Rectangle(90,10)
r2.perimeter()
r2.area()


輸出·

周長(zhǎng)是100
面積是600
周長(zhǎng)是200
面積是900

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


class Student:
    students=[]

    def __init__(self,name,age,id):
        self.name=name
        self.age = age
        self.id = id
        stu={'name':self.name,'age':self.age,'id':self.id}
        Student.students.append(stu)

    @classmethod
    def show_stu(cls):
        for x in Student.students:
            print(x)

    @classmethod
    def answer(self):
        is_arrive=int(input('到達(dá)請(qǐng)輸入1:'))
        if is_arrive==1:
            return True
        else:
            return False

class Class:
    def __init__(self,students,class_name):
        self.students = students
        self.class_name = class_name

    @classmethod
    def add_stu(cls,name,age,id):
        stu=Student(name,age,id)

    @classmethod
    def del_stu(self):
        id=int(input('請(qǐng)輸入學(xué)號(hào):'))
        for x in Student.students[:]:
            if x['id']==id:
                Student.students.remove(x)

    @classmethod
    def ask_stu(self):
        name=input('學(xué)生姓名:')
        if Student.answer():
            print('到')
        else:
            print('沒到')


stu1=Student('aaa',19,12345)
stu2=Student('aab',14,12346)
Student.show_stu()
 print(‘==========================’)
Class.add_stu('aac',18,123457)
Student.show_stu()
Class.del_stu()
Student.show_stu()
Class.ask_stu()


輸出
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
=============================
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
{'name': 'aac', 'age': 18, 'id': 123457}
請(qǐng)輸入學(xué)號(hào):123457
{'name': 'aaa', 'age': 19, 'id': 12345}
{'name': 'aab', 'age': 14, 'id': 12346}
學(xué)生姓名:aaa
到達(dá)請(qǐng)輸入1:1
到
到
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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