day16_FinalCardTestEx03

육희영·2021년 10월 28일
0
package com.java1.day16;

//final - 마지막의, 변경될 수 없는
class Card { 
    final int NUMBER;             // 상수지만 선언과 함께 초기화 하지 않고 
    final String KIND;             // 생성자에서 단 한번만 초기화할 수 있다. 
    static int width = 100;       
    static int height = 250; 

    Card(String kind, int num) {       // 매개변수로 넘겨받은 값으로 상수인  KIND와 NUMBER를 초기화한다. 
          KIND = kind; 
          NUMBER = num; 
    } 

    Card() { 
          this("HEART", 1); 
    } 
    
    @Override
    public String toString() {
    	return KIND + " " + NUMBER;
    }
} 

public class FinalCardTestEx03 {
	public static void main(String[] args) {
		 Card c = new Card("HEART", 10); // 생성자를 통해서 상수를 초기화 한다.
//       c.NUMBER = 5;       에러발생! cannot assign a value to final variable NUMBER 
         System.out.println(c.KIND); 
         System.out.println(c.NUMBER); 
//		 System.out.println(c); 는 System.out.println(c.toString()); 와 같다.
	}
}


/*final class FinalTest {	//조상이 될수 없는 클래스. 상속을 허용하지 않는 클래스
  	final int MAX_SIZE = 10;	// 값을 변경할 수 없는 멤버변수(상수)
  	
  	final void getMaxSize(){	// 오버라이딩할 수 없는 메서드(변경불가)
  		final int LV = MAX_SIZE;	//값을 변경 할수 없는 지역변수(상수)
  		return MAX_SIZE;		
  	}
  }
 */

출력결과

HEART
10

0개의 댓글

관련 채용 정보