Day12-作業(yè)

5.寫一個類,封裝所有和數(shù)學運算相關(guān)的功能

    class MathMethod:
        def __init__(self,num1=1,num2=1):
            self.num1=num1
            self.num2=num2
        def choose_method(self,type):
            if type=='+':
                sum=self.add_method()
                return sum
            if type=='-':
                sum=self.sub_method()
                return sum
            if type=='*':
                sum=self.ride_method()
                return sum
            if type=='/':
                sum=self.except_method()
                return sum
            if type=='%':
                sum=self.savings_method()
                return sum
            if type=="http://":
                sum=self.rectify_method()
                return sum
            if type=="**":
                sum=self.exp_method()
                return sum
        def add_method(self):
            return self.num1+self.num2
        def sub_method(self):
            return self.num1-self.num2
        def ride_method(self):
            return self.num1*self.num2
        def except_method(self):
            if self.num2!=0:
                return self.num1/self.num2
            return False
        def savings_method(self):
            if self.num2!=0:
                return self.num1%self.num2
            return False
        def rectify_method(self):
            if self.num2!=0:
                return self.num1//self.num2
            return False
        def exp_method(self):
            return self.num1**self.num2
    method=MathMethod(2,3)
    print(method.choose_method('+'))
    print(method.choose_method('-'))
    print(method.choose_method('*'))
    print(method.choose_method('/'))
    print(method.choose_method('%'))
    print(method.choose_method('//'))
    print(method.choose_method('**'))

5
-1
6
0.6666666666666666
2
0
8

# 1.聲明一個電腦類:
    # 屬性:品牌鹦蠕、顏色冒签、內(nèi)存大小
    # 方法:打游戲、寫代碼钟病、看視頻
    # a.創(chuàng)建電腦類的對象萧恕,然后通過對象點的方式獲取、修改肠阱、添加和刪除它的屬性
    # b.通過attr相關(guān)方法去獲取票唆、修改、添加和刪除它的屬性
    class Computer:
        __slots__ = ('brand','color','mem_size','money','address')
        def __init__(self,brand,color,mem_size):
            self.brand=brand
            self.color=color
            self.mem_size=mem_size
        def play_game(self):
            print('可以用來打游戲')
        def write_code(self):
            print('可以用來寫代碼')
        def look(self):
            print('可以播放電視')
    ap_computer=Computer('蘋果','red','8G')
    print(ap_computer.color)
    ap_computer.color='blue'
    print(ap_computer.color)
    ap_computer.address='美國'
    print(ap_computer.address)
    del ap_computer.address
    # print(ap_computer.address)
    print(ap_computer.__getattribute__('mem_size'))
    print(getattr(ap_computer,'color'))
    ap_computer.__setattr__('mem_size','16G')
    print(ap_computer.mem_size)
    setattr(ap_computer,'color','green')
    print(ap_computer.color)
    ap_computer.__setattr__('money','10000元')
    print(ap_computer.money)
    delattr(ap_computer,'money')
    # print(ap_computer.money)
    # ap_computer.__delattr__('money')
    # print(ap_computer.money)

red
blue
美國
8G
blue
16G
green
10000元
2.聲明一個人的類和狗的類:狗的屬性:名字屹徘、顏色走趋、年齡,狗的方法:叫喚噪伊,
人的屬性:名字簿煌、年齡、狗鉴吹,人的方法:遛狗
a.創(chuàng)建人的對象小明啦吧,讓他擁有一條狗大黃,然后讓小明去遛大黃

class Dog:
    __slots__ = ('dog_name',"color", 'dog_age')
    def __init__(self, name,color, age):
        self.dog_name=name
        self.color=color
        self.dog_age=age
    def dog_call(self):
        return '%s正在旺旺大叫'%(self.dog_name)
    def __str__(self):
        return 'dog_name:%s color:%s dog_age:%d'%(self.dog_name,self.color,self.dog_age)

class Person:
    __slots__ = ('name','age','dog')
    def __init__(self,name,age,dog=[]):
        self.name=name
        self.age=age
        self.dog=dog
    def get_dog(self,name,color, age):
        dog1=Dog(name,color, age)
        self.dog.append(dog1)
        print('%s剛得到一條狗%s'%(self.name,dog1.dog_call()))
    def walk_dog(self):
        dog_name=self.dog[0].dog_name
        return '%s正在遛他剛得到的%s'%(self.name,self.dog[0].dog_name)
p=Person('小明',20)
p.get_dog('大黃','yellow',3)
print(p.walk_dog())

3.聲明一個矩形類:

屬性:長拙寡、寬

方法:計算周長和面積

a.創(chuàng)建不同的矩形授滓,并且打印其周長和面積

class Rect:
    def __init__(self,height,width):
        self.height=height
        self.width=width
    def perimeter(self):
        return 2*(self.width+self.height)
    def area(self):
        return self.height*self.width
rect1=Rect(4,5)
print(rect1.perimeter(),rect1.area())
rect1=Rect(10,3)
print(rect1.perimeter(),rect1.area())
rect1=Rect(15,15)
print(rect1.perimeter(),rect1.area())

18 20
26 30
60 225

4.

創(chuàng)建一個學生類:

屬性:姓名,年齡肆糕,學號

方法:答到般堆,展示學生信息

創(chuàng)建一個班級類:

屬性:學生,班級名

方法:添加學生诚啃,刪除學生淮摔,點名

class Student:
    def __init__(self,name,age,id):
        self.student_name=name
        self.student_age=age
        self.student_id=id
    def answer(self):
        print('%s到'%(self.student_name))
    def check(self):
        print('student_name:%s student_age:%s student_id:%s'\
               %(self.student_name,self.student_age,self.student_id))
    def __str__(self):
        return 'student_name:%s student_age:%s student_id:%s'\
               %(self.student_name,self.student_age,self.student_id)
class Class:
    def __init__(self,grade,student=[]):
        self.student=student[:]
        self.grade=grade
    def add_student(self,name,age):
        id=''
        try:
            max=self.student[0].student_id
            for item in self.student:
                if max<item.student_id:
                    max=item.student_id
            str1=int(max[-1])
            str2=int(max[-2])
            str3=int(max[-3])
            if str1+1>9:
                if str2+1>9:
                    str3+=1
                else:
                    str2+=1
            else:
                str1+=1
            id += 'py1805' + str(str3) + str(str2) + str(str1)
        except IndexError:
            id+='py1805001'
        student1=Student(name,age,id)
        self.student.append(student1)
        student1.check()
    def del_student(self):
        list1=[]
        del_student=input('請輸入要刪除學生的姓名或則id')
        for item in self.student[:]:
            # print(item.student_id,del_student)
            id=item.student_id
            if del_student==id:
                self.student.remove(item)
                return
            elif del_student==item.student_name:
                list1.append(item)
        for x in range(len(list1)):
            print(x,list1[x])
        del_name=int(input('請輸入選擇'))
        self.student.remove(list1[del_name])
        for item in self.student:
            print(item)

    def check_student(self):
        for item in self.student:
            print(item)
        check_name=input('輸入姓名:')
        for item in self.student:
            if check_name==item.student_name:
                student2=Student(item.student_name,item.student_age,item.student_id)
                student2.answer()
c1=Class('1班')
c1.add_student('小明',20)
c1.add_student('小李',21)
c1.del_student()
c1.add_student('小廖',22)
print(c1.student[0])
c1.del_student()
print(c1.student[0])
c1.check_student()

D:\Python項目\Day12-面向?qū)ο骪object\venv\word.py\Scripts\python.exe D:/Python項目/Day12-面向?qū)ο?object/作業(yè).py
student_name:小明 student_age:20 student_id:py1805001
student_name:小李 student_age:21 student_id:py1805002
請輸入要刪除學生的姓名或則id小明
0 student_name:小明 student_age:20 student_id:py1805001
請輸入選擇0
student_name:小李 student_age:21 student_id:py1805002
student_name:小廖 student_age:22 student_id:py1805003
student_name:小李 student_age:21 student_id:py1805002
請輸入要刪除學生的姓名或則idpy1805002
student_name:小廖 student_age:22 student_id:py1805003
student_name:小廖 student_age:22 student_id:py1805003
輸入姓名:小廖
小廖到

6.1.寫一個班級類,屬性:班級名始赎、學生和橙;功能:添加學生仔燕、刪除學生、根據(jù)姓名查看學生信息魔招,

展示班級的所有學生信息

class Class:
    def __init__(self,grade,student=[]):
        self.student=student
        self.grade=grade
    def add_student(self,name,age):
        id=''
        dict1={}
        try:
            max=self.student[0]['student_id']
            for item in self.student:
                if max<item['student_id']:
                    max=item['student_id']
            str1=int(max[-1])
            str2=int(max[-2])
            str3=int(max[-3])
            if str1+1>9:
                if str2+1>9:
                    str3+=1
                else:
                    str2+=1
            else:
                str1+=1
            id += 'py1805' + str(str3) + str(str2) + str(str1)
        except :
            id+='py1805001'
        dict1['student_name']=name
        dict1['student_age'] = age
        dict1['student_id'] = id
        self.student.append(dict1)
    def del_student(self):
        list1=[]
        del_student=input('請輸入要刪除學生的姓名或則id')
        for item in self.student[:]:
            # print(item.student_id,del_student)
            id=item['student_id']
            if del_student==id:
                self.student.remove(item)
                return
            elif del_student==item['student_name']:
                list1.append(item)
        for x in range(len(list1)):
            print(x,list1[x])
        del_name=int(input('請輸入選擇'))
        self.student.remove(list1[del_name])
        for item in self.student:
            print(item)
    def check_all(self):
        for item in self.student:
            print(self.grade,'姓名:%s 年齡:%s id:%s' % (item['student_name'], item['student_age'], item['student_id']))
    def check_student(self):
        check_name=input('輸入姓名:')
        for item in self.student:
            if item['student_name']==check_name:
                return '姓名:%s 年齡:%s id:%s'%(item['student_name'],item['student_age'],item['student_id'])
c=Class('1班')
c.add_student('小明',20)
c.add_student('小李',22)
# print(c.check_student())
c.check_all()
c.del_student()
c.check_all()

1班 姓名:小明 年齡:20 id:py1805001
1班 姓名:小李 年齡:22 id:py1805002
請輸入要刪除學生的姓名或則id小明
0 {'student_name': '小明', 'student_age': 20, 'student_id': 'py1805001'}
請輸入選擇0
{'student_name': '小李', 'student_age': 22, 'student_id': 'py1805002'}
1班 姓名:小李 年齡:22 id:py1805002

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末晰搀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子办斑,更是在濱河造成了極大的恐慌外恕,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乡翅,死亡現(xiàn)場離奇詭異鳞疲,居然都是意外死亡,警方通過查閱死者的電腦和手機蠕蚜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門尚洽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人靶累,你說我怎么就攤上這事腺毫。” “怎么了尺铣?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵拴曲,是天一觀的道長。 經(jīng)常有香客問我凛忿,道長澈灼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任店溢,我火速辦了婚禮叁熔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘床牧。我一直安慰自己荣回,他們只是感情好,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布戈咳。 她就那樣靜靜地躺著心软,像睡著了一般。 火紅的嫁衣襯著肌膚如雪著蛙。 梳的紋絲不亂的頭發(fā)上删铃,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音踏堡,去河邊找鬼猎唁。 笑死,一個胖子當著我的面吹牛顷蟆,可吹牛的內(nèi)容都是我干的诫隅。 我是一名探鬼主播腐魂,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼逐纬!你這毒婦竟也來了蛔屹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤风题,失蹤者是張志新(化名)和其女友劉穎判导,沒想到半個月后嫉父,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沛硅,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年绕辖,在試婚紗的時候發(fā)現(xiàn)自己被綠了摇肌。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡仪际,死狀恐怖围小,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情树碱,我是刑警寧澤肯适,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站成榜,受9級特大地震影響框舔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赎婚,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一刘绣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧挣输,春花似錦纬凤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至完丽,卻和暖如春恋技,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背舰涌。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工猖任, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓷耙。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓朱躺,卻偏偏與公主長得像刁赖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子长搀,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容