日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python学习9 面向对象 类和对象

發布時間:2023/12/13 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python学习9 面向对象 类和对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

面向對象和面向過程




類和對象



類的設計


類的創建


self:相當于當前對象;類似于Java中的this


類的創建和使用:

#類的命名:每個單詞首字母大寫 class Dog:#屬性name = 'dog'age = 11#方法def eat(self):print('eat rice!')dog = Dog() print(dog.name,dog.age,sep='\n') dog.eat()class Dog1(object):name = 'dog'age = 11 #構造方法def __init__(self, name, age):self.name = nameself.age = agedef mood(self):print('good')dog1 = Dog1('dog1', 8) print(dog1.name,dog1.age,sep='\n') dog1.mood()

基本語法:self


基本語法:slots


案例:

#3 class Choose(object):#規定對象的屬性,不能隨意增加屬性__slots__ = ('name','age','position')def __init__(self,name,age,postion):self.name=nameself.age=ageself.position=postiondef fun(self):print('just fun,don\'t think!')def printName(self):print('name={}'.format(self.name)) c=Choose('c',90,'position') c.fun() c.printName()class Choose(object):def __init__(self,name,age,postion):self.name=nameself.age=ageself.position=postion c=Choose('c',90,'position') c.city='add'#不存在,就添加屬性;存在,就修改屬性值 print(c.city)

魔術方法

魔術方法:__init__方法 & __del__方法



魔術方法:__str__方法


魔術方法: __call__方法

魔術方法: __eq__方法



魔術方法案例

# 4 魔術方法:自動執行 class Magic(object):__slots__ = ('name', 'age')# 創建時調用def __init__(self, name, age):self.name = nameself.age = ageprint('init class!:' + self.name)# 在銷毀對象時調用def __del__(self):print('delete class!:' + self.name)# 在默認輸出對象時調用,默認打印當前對象地址;重寫后打印信息def __str__(self):return 'this is str:' + self.name# 在m1()時調用def __call__(self, *args, **kwargs):print('call class:' + self.name)# 在==比較對象時調用def __eq__(self, other):return self.name == other.name and self.age == other.agem1 = Magic('m1', 1) m2 = Magic('m2', 2) # 調用str print(m1) print(m2) # 調用call m1() m2() # 比較兩個對象的內存地址 print(m1 is m2) # 調用eq進行比較 print(m1 == m2)#輸出 #init class!:m1 # init class!:m2 # this is str:m1 # this is str:m2 # call class:m1 # call class:m2 # False # False # delete class!:m1 # delete class!:m2

其他魔術方法總結


練習1-擺放家具



代碼

#author:dq #project:PythonProject #date:2021年10月18日 #function:class # 編程題:【擺放家具】需求: # 1 房子(House)有戶型、總面積、剩余面積(=總面積-家具面積) 和 家具名稱列表 屬性。 # 注:新房子沒有任何家具 # 2 家具(HouseItem)有 名字 和 占地面積 屬性,其中 # 席夢思(bed)占地 4 平米 # 衣柜(chest)占地 2 平米 # 餐桌(table)占地 1.5 平米 # 3 將以上三件家具添加到房子中; # 4 判斷 家具的面積 是否 超過剩余面積,如果超過,提示不能添加這件家具; # 5 打印房子時,要求輸出:戶型、總面積、剩余面積、家具名稱列表class House(object):__slots__ = ('style', 'allSzie', 'restSize', 'furnituresList')def __init__(self, style, allSzie, restSize, furnitureList):self.style = styleself.allSzie = allSzieself.restSize = restSizeself.furnituresList = furnitureListdef __str__(self):#print('style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+str(self.furnituresList) + '\n')#return 'style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+str(self.furnituresList) + '\n'return 'style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+' '.join(self.furnituresList) + '\n'def add_furniture(self, item):if(item.area<=self.restSize):self.restSize-=item.areaself.furnituresList.append(item.name)print('可以添加'+item.name+'家具!\n')else:print('不可以添加'+item.name+'家具!\n')class HouseItem(object):__slots__ = ('name', 'area')def __init__(self, name, area):self.name = nameself.area = areadef __str__(self):return 'name='+self.name+'\t'+'area='+str(self.area)+'\n'bed=HouseItem('席夢思',4) chest=HouseItem('衣柜',2) table=HouseItem('餐桌',1.5) print(bed) print(chest) print(table) h=House('別墅',10,10,[]) h.add_furniture(bed) h.add_furniture(chest) h.add_furniture(table) print(h)

練習2-計算點是否在矩形內

#author:dq #project:PythonProject #date:2021年10月18日 #function: # 需求:設計兩個類: # 一個點Point類,屬性包括 x 和 y 坐標。 # 一個矩形Rectangle類,屬性有左上角(top_left)和 右下角(bottom_right)的坐標。 # 方法:1.計算矩形的面積(get_area);2.判斷點是否在矩形內(is_inside)。 # 主程序: # 實例化一個點對象,一個正方形對象,輸出矩形的面積,輸出點是否在矩形內class Point(object):__slots__ = ('x','y')def __init__(self,x,y):self.x=xself.y=ydef __str__(self):return 'x='+self.x+'\t'+'y='+self.y+'\n' class Rectangle(object):__slots__ = ('x1', 'y1','x2','y2')def __init__(self, x1, y1,x2,y2):self.x1 = x1self.y1 = y1self.x2 = x2self.y2 = y2def __str__(self):return 'area='+ str(self.area())def area(self):return (self.x2-self.x1)*(self.y2-self.y1)def inside(self,point):if((self.x1<point.x<self.x2) and(self.y1<point.y<self.y2)):return Trueelse:return False point=Point(1,4) r=Rectangle(0,0,2,7) print(r) print(r.inside(point))

練習3-計算器類

設計一個計算器類,可以進行加、減、乘、除計算。

# author:dq # project:PythonProject # date:2021年10月18日 # function:設計一個計算器類,可以進行加、減、乘、除計算。class Count(object):__slots__ = ('x', 'pos', 'y')def __init__(self, x, pos, y):self.x = xself.pos = posself.y = ydef decide(self):if (self.pos == '+'):return self.add()elif (self.pos == '-'):return self.sub()elif (self.pos == '*'):return self.mul()elif (self.pos == '/'):return self.truediv()def add(self):return self.x + self.ydef sub(self):return self.x - self.ydef mul(self):return self.x * self.ydef truediv(self):return self.x / self.ydef __str__(self):return str(self.x) + self.pos + str(self.y) + "=" + str(self.decide())x=int(input('請輸入x:')) pos=input('請輸入pos:') y=int(input('請輸入y:')) c = Count(x, pos, y) print(c)

總結

以上是生活随笔為你收集整理的Python学习9 面向对象 类和对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。