JAVA/기본 이론

JAVA | static

로아다 2023. 3. 24. 16:05
728x90
반응형
static (정적 영역, 클래스 영역 <=> 인스턴스 영역)

 - 인스턴스 영역의 반대 개념이다.

 - 같은 클래스의 모든 인스턴스들이 함께 사용하는 공동 영역을 정적 영역(클래스 영역)이라고 한다.

 - 앞에 static이 붙은 자원들은 모든 인스턴스들이 함께 사용하는 공동 자원이 된다.

import myobj.CartClanMember;
import myobj.Sword;

public class C05_Static {
        
        public static void main(String[] args) {
                
                Sword s1 = new Sword("녹슨 검");
                Sword s2 = new Sword("여행자의 검");
                
                for (int i = 0; i < 10; ++i) {
                        s1.swing();
                }
                
                System.out.println("칼1의 내구도 : " + s1.durability);
                System.out.println("칼2의 내구도 : " + s2.durability);
                
                s1.attack = 55;
                
                System.out.println(s1.attack);
                System.out.println(s2.attack);
                
package myobj;

public class Sword {
	
	public static int attack = 10; // s1, s2 값이 공동임
	
	String name;
	public int durability;
	
	public Sword(String name) {
		this.name = name;
		durability = 10;
	}
	
	/** 이 칼을 휘두른다. (내구도 -1) */
	public void swing() {
		if (--durability == 0) {
			System.out.println("\"" + this.name + "\"이(가) 파괴되었습니다!");
		} else {
			System.out.println("\"" + this.name + "\"을(를) 휘둘렀습니다.");
		}
	}
}

 

 

 

Quiz

(1) 붕어빵을 100개 만들고 어떤 붕어빵이 몇 개 있는지 세어보세요 (단, 붕어빵의 맛은 만들 때 랜덤으로 결정되어야 함)

(2) 붕어빵 배열을 전달하면 모든 붕어빵의 가격이 얼마인지 계산해주는 메서드를 만들어보세요

※ 붕어빵의 맛 종류 - 슈크림, 팥, 초코

※ 붕어빵의 가격 - 팥 800, 슈크림 1000, 초코 1200

※ 각 붕어빵에는 품질이 있으면 품질에 따라 +-50원이 된다. (상/중/하)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Answer
package answer;

import myobj.FishBread2;

public class C05_CountFishBread {
	/*
	(1) 붕어빵을 100개 만들고 어떤 붕어빵이 몇 개 있는지 세어보세요
	    (단, 붕어빵의 맛과 품질은 만들 때 랜덤으로 결정되어야 함)

 	(2) 붕어빵 배열을 전달하면 모든 붕어빵의 가격이 얼마인지 계산해주는
 	    함수를 만들어보세요

 	    ※ 붕어빵의 맛 종류 - 슈크림, 팥, 초코
 	    ※ 붕어빵의 가격 - 팥 800, 슈크림 1000, 초코 1200
 		※ 각 붕어빵에는 품질이 있으면 품질에 따라 +-50원이 된다. (상/중/하)
	 */
	public static int totalPrice(FishBread2[] fishes) {
		int sum = 0;
		for (int i = 0; i < fishes.length; ++i) {
			sum += fishes[i].getPrice();
		}
		return sum;
	}
	
	public static void main(String[] args) {
		
		FishBread2[] fishes = new FishBread2[100]; // 배열 생성
		
		for (int i = 0; i < 100; ++i) {
			
			fishes[i] = new FishBread2(); // 생성자 호출
			fishes[i].info();
		}
		System.out.println("총 가격: " + totalPrice(fishes));
	}
}
package myobj;

public class FishBread2 {
	
	static String[] tastes = {"팥", "슈크림", "초코"};
	static int[] prices = {800, 1000, 1200};
	static String[] qualityNames = {"상급 붕어빵", "중급 붕어빵", "하급 붕어빵"};
	static int[] qualityPrices = {50, 0, -50};
	
	String taste;
	int price;
	int quality;
	
	/** 맛과 품질을 랜덤으로 결정 */
	public FishBread2() {
		int type = (int)(Math.random() * tastes.length);
		this.taste = tastes[type];
		this.price = prices[type];
		this.quality = (int)(Math.random() * qualityNames.length);
	}
	
	/** 맛과 품질을 직접 설정 */
	public FishBread2(String taste, int price, int quality) {
		this.taste = taste;
		this.price = price;
		this.quality = quality;
	}
	
	/** 품질에 따른 붕어빵의 가격을 나타내는 메서드 */
	public int getPrice() {
		return this.price + qualityPrices[this.quality];
	}
	
	/** 붕어빵의 정보 */
	public void info() {
		System.out.printf("(%s, %d, %s)\n", taste, getPrice(), qualityNames[quality]);
	}
}
728x90
반응형