ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Power JAVA 9장 ) 생성자와 접근 제어 - Programming_2
    IT/솔루션) Power Java 2019. 5. 12. 17:42

    <2016. 10. 3. 18:59>

     

    Power JAVA 9장 ) 생성자와 접근 제어 - Programming_2

     

    /* 
     * 비행기를 나타내는 Plane라는 이름의 클래스를 설계하라.
     * Plane 클래스는 제작사(예를 들어 에어버스), 모델(A380), 최대 승객수(500) 필드를 가지고 있다.
     *  - 필드를 정의하라. 모든 필드는 전용 멤버로 하라.
     *  - 모든 필드에 대한 접근자와 설정자 메소드를 작성한다.
     *  - Plane 클래스의 생성자를 몇 개를 중복 정의하라. 생성자는 모든 데이터를 받을 수도 있고 아니면 하나도 받지 않을 수 있다.
     *  - PlaneTest라는 이름의 테스트를 클래스를 만드는데 main()에서 plane 객체 여러 개를 생성하고 접근자와 설정자를 호출하여 보라.
     *  - Plane 클래스에 지금까지 생성된 비행기의 개수를 나타내는 정적 변수인 planes를 추가하고 생성자에서 증가시켜 보자.
     *  - Plane 클래스에 정적 변수 planes의 값을 반환하는 정적 메소드인 getPlanes()를 추가하고  main 메소드에서 호출하여 보라.
     */
    class Plane {
    	private String company;
    	private String model;
    	private int max_cus;
    	static int planes = 0;
    	Plane() {
    		this("Unknow","Unknow",0);
    	}
    	Plane(String company,String model, int max_cus) {
    		this.company = company;
    		this.model = model;
    		this.max_cus = max_cus;
    		planes++;
    	}
    	void setCompany(String compnay) {
    		this.company = compnay;
    	}
    	void setModel(String model) {
    		this.model = model;
    	}
    	void setMax_cus(int max_cus) {
    		this.max_cus = max_cus;
    	}
    	public String toString() {
    		return company + "," + model +","+ max_cus;
    	}
    	static int getPlanes() {
    		return planes;
    	}
    }
    public class Programming_2 {
    	public static void main(String[] args) {
    		Plane myplane1 = new Plane();
    		System.out.println(myplane1.toString());
    		System.out.println("현재까지 만들어진 비행기 수 : " + Plane.planes);
    		Plane myplane2 = new Plane("대한항공","A480",500);
    		System.out.println(myplane2.toString());
    		System.out.println("현재까지 만들어진 비행기 수 : " + Plane.planes);
    		System.out.println(Plane.getPlanes());
    	}
    }

    댓글

다치지 말고 운동하자.