Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

클래스를 만들고 원의 면적과 둘레를 계산하는 Python 프로그램

<시간/>

클래스를 이용하여 원의 넓이와 둘레를 구해야 할 때 객체지향 방법을 사용한다. 여기에서 클래스가 정의되고 속성이 정의됩니다. 함수는 특정 작업을 수행하는 클래스 내에서 정의됩니다. 클래스의 인스턴스가 생성되고 함수를 사용하여 원의 면적과 둘레를 찾습니다.

아래는 동일한 데모입니다 -

예시

import math
class circle_compute():
   def __init__(self,my_radius):
      self.radius=my_radius
   def area_calculate(self):
      return math.pi*(self.radius**2)
   def perimeter_calculate(self):
      return 2*math.pi*self.radius
my_result = int(input("Enter the radius of circle..."))
my_instance = circle_compute(my_result)
print("The radius entered is :")
print(my_result)
print("The computed area of circle is ")
print(round(my_instance.area_calculate(),2))
print("The computed perimeter of circle is :")
print(round(my_instance.perimeter_calculate(),2))

출력

Enter the radius of circle...7
The radius entered is :
7
The computed area of circle is
153.94
The computed perimeter of circle is :
43.98

설명

  • 'area_calculate', 'perimeter_calculate'와 같은 기능을 가진 'circle_compute' 클래스가 정의되었습니다.
  • 각각 원의 면적과 둘레를 계산하는 데 사용됩니다.
  • 이 클래스의 인스턴스가 생성됩니다.
  • 반지름 값이 입력되고 작업이 수행됩니다.
  • 관련 메시지 및 출력이 콘솔에 표시됩니다.