day10_CardTestEx01

육희영·2021년 10월 5일
0
package day10;

public class CardTestEx01 {

	public static void main(String[] args) {
				// 클래스변수(static 변수)는 객체생성없이 '클래스이름.클래스변수'로 직접 사용 가능하다. 
		        System.out.println("Card.width = " + Card.width); 
		        System.out.println("Card.height = " + Card.height); 

		        Card c1 = new Card(); 
		        c1.kind = "Heart"; 
		        c1.number = 7; 

		        Card c2 = new Card(); 
		        c2.kind = "Spade"; 	//인스턴스 변수의 값을 변경한다.
		        c2.number = 4; 	    //인스턴스 변수의 값을 변경한다. 

		        System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + Card.width + ", " + Card.height + ")" ); 
		        System.out.println("c2는 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + Card.width + ", " + Card.height + ")" );             
		        System.out.println("이제 c1의 width와 height를 각각 50, 80으로 변경합니다."); 
		        Card.width = 50; 	//클래스 변수의 값을 변경한다.
		        Card.height = 80; //클래스 변수의 값을 변경한다.

		        System.out.println("c1은 " + c1.kind + ", " + c1.number + "이며, 크기는 (" + Card.width + ", " + Card.height + ")" ); 
		        System.out.println("c2는 " + c2.kind + ", " + c2.number + "이며, 크기는 (" + Card.width + ", " + Card.height + ")" ); 
		  } 
		} 

		class Card { 
		 String kind ;                         // 카드의 무늬 - 인스턴스 변수 
		 int number;                         // 카드의 숫자 - 인스턴스 변수 
		 static int width = 100;             // 카드의 폭 - 클래스 변수 
		 static int height = 250;             // 카드의 높이 - 클래스 변수 
		}

출력결과

Card.width = 100
Card.height = 250
c1은 Heart, 7이며, 크기는 (100, 250)
c2는 Spade, 4이며, 크기는 (100, 250)
이제 c1의 width와 height를 각각 50, 80으로 변경합니다.
c1은 Heart, 7이며, 크기는 (50, 80)
c2는 Spade, 4이며, 크기는 (50, 80)

0개의 댓글

관련 채용 정보