day17 物件導向作業

2021-10-25 02:10:07 字數 2610 閱讀 4412

定義乙個矩形類,擁有屬性:長、寬 擁有方法:求周長、求面積

class

rectangular

:def

__init__

(self,x,y)

: self.

long

=x self.width=y

defcalculate

(self)

: l =

(self.

long

+ self.width)*2

s = self.

long

* self.width

return l ,s

p1 = rectangular(10,

10)print

(p1.calculate())

#(40, 100)

定義乙個二維點類,擁有屬性:x座標、y座標 擁有方法:求當前點到另外乙個點的距離

class

point

:def

__init__

(self,n=

0,m=0)

: self.x = n

self.y = m

defdistance

(self, other)

:return

((self.x - other.x)**2

+(self.y - other.y **2)

)**0.5p1 = point(4,

6)p2=point(5,

7)re = p1.distance(p2)

print

(re)

定義乙個圓類,擁有屬性:半徑、圓心 擁有方法:求圓的周長和面積、判斷當前圓和另乙個圓是否外切

class

round

:def

__init__

(self,r,x,y)

: self.r = r

self.x = x

self.y = y

defcalculate

(self,other)

: l =

2*self.r*

3.14

s =3.14

*(self.r)**2

if((self.x - other.x)**2

+(self.y - other.y **2)

)**0.5== self.r+other.r:

d ='外切'

else

: d =

'不外切'

return l,s,d

p1= round(2,

1,1)

p2 = round(3,

1,1)

re = p1.calculate(p2)

print

(re)

定義乙個線段類,擁有屬性:起點和終點, 擁有方法:獲取線段的長度

class

line

:def

__init__

(self,x,y,a,b)

: self.x = x

self.y = y

self.a = a

self.b = b

deflen1

(self)

:return

((self.x - self.a)**2

+(self.y - self.b **2)

)**0.5p1 = line(4,

2,6,

3)re = p1.len1(

)print

(re)

定義乙個狗類和乙個人類:

class

dog:

def__init__

(self,name,***,varieties)

: self.name = name

self.*** = ***

self.varteties = varieties

defcall

(self)

:return f'金叫喚'

class

person

:def

__init__

(self,name,age,dog=

none):

self.name = name

self.age = age

self.dog = dog

defwalk

(self)

:if self.dog:

print

(f'在瘋狂遛'

)p1 = dog(

'來福'

,'公'

,'泰迪'

)print

(p1.call())

p2 = person(

'小明',18

,p1)

p2.walk(

)

狗擁有屬性:姓名、性別和品種 擁有方法:叫喚

人類擁有屬性:姓名、年齡、狗 擁有方法:遛狗

Day17物件導向作業

定義乙個矩形類,擁有屬性 長 寬 擁有方法 求周長 求面積 class rectangle def init self,length,width self.length length self.width width defperimeter self return self.length self...

day17 物件導向作業

定義乙個矩形類,擁有屬性 長 寬 擁有方法 求周長 求面積 class rectangle def init self,length,width self.length length self.width width defperimeter self return self.length 2 se...

day17 物件導向作業

定義乙個矩形類,擁有屬性 長 寬 擁有方法 求周長 求面積 class orthogon def init self,long 0 wide 0 self.long long self.wide wide defperimeter self print 周長 self.long self.wide ...