자바 의 클래스 와 대상


    자바 의 클래스 와 대상 은 C + + 와 유사 하지만 구체 적 으로 사용 할 때 몇 가지 추가 적 인 주의 가 필요 합 니 다.여기 서 나 는 몇 가지 주요 한 것 을 열거 했다. 나중에 발견 하거나 이해 가 더욱 깊 어 지면 다시 추가 했다.
    이 박문 은 또 하나의 중요 한 역할 을 한다. 바로 자바 프로 그래 밍 의 습관 을 확인 하 는 것 이다. 클래스 가 정의 할 때 어떤 주석 을 써 야 하 는 지, 이런 것들 은 모두 좋 은 습관 을 가 져 야 한다.
1) 하나의 자바 파일 에 여러 개의 클 라 스 가 존재 할 수 있 지만, 하나의 Public class + 파일 이름과 같은 클래스 만 있 을 수 있 습 니 다.이 클래스 는 주 클래스 입 니 다. 이름 은 파일 이름과 일치 해 야 합 니 다.
2) 주 클래스 에서 만 Public static void main (string [] args) {} 을 정의 할 수 있 습 니 다. main 방법 이 있어 야 주 클래스 가 실 행 됩 니 다.
3) 클래스 가 정 의 될 때 구조 함수 도 다시 불 러 올 수 있다.
    기본 코드 는 다음 과 같 습 니 다.
/**
 * 
 * @author Powered by Zhu Yangping
 *
 */

class Circle {
	/**
	 * CIRCLE CLASS                
	 * 
	 * Data: radius
	 * 
	 * Functions: getCircum, getArea
	 */
	
	// data
	double radius; 
	
	// constructor function 1
	Circle() {
		radius = 1.0; 
	}
	
	// constructor function 2
	Circle(double newRadius) {
		radius = newRadius; 
	}
	
	// getCircum function
	double getCircum() {
		return 2 * radius * Math.PI; 
	}
	
	// getArea function
	double getArea() {
		return radius * radius * Math.PI; 
	}
}




public class TestCircle {

	/**
	 * @param args              
	 * 
	 * MAIN CLASS
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Circle circle1 = new Circle(); 
		System.out.println("The circum of circle1 is " + circle1.getCircum() + ", and its area is " + circle1.getArea()); 
		
		Circle circle2 = new Circle(25.0);
		System.out.println("The circum of circle1 is " + circle2.getCircum() + ", and its area is " + circle2.getArea());
		
		Circle circle3= new Circle(10);
		System.out.println("The circum of circle1 is " + circle3.getCircum() + ", and its area is " + circle3.getArea());
		
		
		
	}

}

좋은 웹페이지 즐겨찾기