day 17 物件導向作業
class Ractangle:
def __init__(self,width,length):
self.width=width
self.length=length
def perimeter(self):
return 2*(self.width+self.length)
def area(self):
return self.width*self.length
r1=Ractangle(10,8)
result1=r1.perimeter()
result2=r1.area()
print(f'周長為{result1},面積為{result2}')
class Point:
def __init__(self,x,y):
self.x=x
self.y=y
def distance(self,other):
return ((self.x-other.x)**2+(self.y-other.y)**2)**0.5
p1=Point(10,20)
p2=Point(5,8)
result=p1.distance(p2)
print(result)
"""
兩個圓只有一個公共點,且圓心的距離等於兩個圓半徑的和,這兩個圓互為外切圓。
"""
class Circle:
pi=3.1415926
def __init__(self,r,x,y):
self.r=r
self.x=x
self.y=y
def perimeter_circle(self):
return 2*Circle.pi*self.r
def area_circle(self):
return Circle.pi*(self.r)**2
def ex_tangent_circle(self,other):
if ((self.x-other.x)**2+(self.y-other.y)**2)**0.5==float(other.r+self.r):
return '互為外切圓'
else:
return '不外切'
c1=Circle(2,1,1)
c2=Circle(3,6,1)
result=c1.ex_tangent_circle(c2)
result1=c1.perimeter_circle()
result2=c1.area_circle()
result3=c2.perimeter_circle()
result4=c2.area_circle()
print(f'c1的周長為{result1}面積為{result2};c2的周長為{result3}面積為{result4},c1與c2{result}')
class Line:
def __init__(self,start_x,start_y,end_x,end_y):
self.start_x=start_x
self.start_y=start_y
self.end_x=end_x
self.end_y=end_y
def length(self):
return ((self.start_x-self.end_x)**2+(self.start_y-self.end_y)**2)**0.5
l1=Line(1,1,5,6)
result=l1.length()
print(result)
class Dog:
def __init__(self,name,gender,breed):
self.name=name
self.gender=gender
self.breed=breed
def bark(self):
print('汪汪~')
class Humanbeings:
def __init__(self,name,age,dog):
self.name=name
self.age=age
self.dog=dog
def play(self):
print('遛狗')