定義乙個矩形類,擁有屬性:長、寬 擁有方法:求周長、求面積
class
rect
:def
__init__
(self,
long=10
, weight=10)
: self.
long
=long
self.width = weight
# 求周長
defperimeter
(self)
:print
(self.
long*2
+ self.width*2)
# 求面積
defarea
(self)
:print
(self.
long
*self.width)
定義乙個二維點類,擁有屬性:x座標、y座標 擁有方法:求當前點到另外乙個點的距離
class
twopoint
:def
__init__
(self, x1=
1, y1=1)
: self.x1 = x1
self.y1 = y1
defdistance
(self, x2=
1, y2=1)
:print((
(self.x1-x2)**2
+(self.y1-y2)**2
)**0.5)
定義乙個圓類,擁有屬性:半徑、圓心 擁有方法:求圓的周長和面積、判斷當前圓和另乙個圓是否外切
import math
class
circle
:def
__init__
(self, long1=
1, x1=
1, y1=1)
: self.long1 = long1
self.x1 = x1
self.y1 = y1
defperimeter
(self)
: per =
2*self.long1*math.pi
print
(per)
defarea
(self)
: area1 = math.pi*self.long1**
2print
(area1)
defjudge
(self, long2=
1, x2=
1, y2=1)
:if((self.x1-x2)**2
+(self.y1-y2)**2
)**0.5== self.long1+long2:
print
('兩個圓外切'
)else
:print
('兩個圓不外切'
)
定義乙個線段類,擁有屬性:起點和終點, 擁有方法:獲取線段的長度
class
line
:def
__init__
(self, x1=
1, y1=
1, x2=
1, y2=1)
: self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
defline_long
(self)
: l_l =
((self.x1-self.x2)**2
+(self.y1-self.y2)**2
)**0.5print
(l_l)
定義乙個狗類和乙個人類:
狗擁有屬性:姓名、性別和品種 擁有方法:叫喚
人類擁有屬性:姓名、年齡、狗 擁有方法:遛狗
class
dog:
def__init__
(self, name=
'小花'
, ***=
'母', variety=
'金毛'):
self.name = name
self.*** = ***
self.variety = variety
defcall
(self)
:print
('叫喚'
)class
person
:def
__init__
(self, name=
'李', age=
18, pet=
'狗')
: self.name = name
self.age = age
self.pet = pet
defwalk_the_dog
(self)
:print
('遛狗'
)
day16 物件導向作業
定義乙個矩形類,擁有屬性 長 寬 擁有方法 求周長 求面積 class rect def init self,l 10,w 10 self.length l self.width w defarea self print f 面積 def perimiter self print f 周長 定義乙個...
day16 物件導向作業
定義乙個矩形類,擁有屬性 長 寬 擁有方法 求周長 求面積 class rectangle def init self,long 10 wide 10 self.long1 long self.wide1 wide defarea self print f 面積 def perimeter self...
day16 物件導向
一.物件導向思想 1.面向過程.重點在 過程 按照實物的發展流程.先幹嘛,後幹嘛,最後幹嘛.優點 簡單.流水線式的工作 缺點 可擴充套件性差.2.物件導向.什麼是物件?物件是屬性和動作的集合體.物件是乙個抽象的概念.重點在 物件 屬性和動作.類似上帝式的思維 優點 可擴充套件性強 多型 缺點 編寫相...