λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μ‹œλ„/ꡭ비지원

[D+68] enum, Annotation, κ°€λ³€μΈμž, Nested Class, Inner Class

by πŸ‡λ°•λ΄‰λ΄‰πŸ‡ 2022. 12. 27.

 

 

ꡭ비지원 D+68

-  enum, Annotation, κ°€λ³€μΈμž, Nested Class, Inner Class -

 


 

enum

- enumeration μ—΄κ±°ν˜• 데이터 νƒ€μž…

- μ—΄κ±°ν˜• λ°μ΄ν„°νƒ€μž…μ€ λ°μ΄ν„°μ˜ λ²”μœ„λ₯Ό μ—΄κ±°μ‹œν‚¨ λ°μ΄ν„°λ‘œ ν•œμ •μ‹œν‚€λŠ” λ°μ΄ν„°νƒ€μž…μ΄λ‹€.

 

• κΈ°μ‘΄ μΈν„°νŽ˜μ΄μŠ€λ₯Ό enumμ—­ν•  μ‚¬μš©μ‹œ 문제점

- μΈν„°νŽ˜μ΄μŠ€μ˜ μ›λž˜μš©λ„μ™€ 동떨어진 μ‚¬μš©μœΌλ‘œ μΈν„°νŽ˜μ΄μŠ€κ°€ μ˜€μš©λœλ‹€.

- λ³€μˆ˜κ°’μ˜ λ²”μœ„μ— λŒ€ν•΄ μ œν•œμ„ λ‘˜ 수 μ—†λ‹€. (μ œν•œμ—†μ΄ λ‹€λ₯Έ 값이 λ³€μˆ˜κ°’μ— 듀어가도 λ¬Έμ œκ°€ λ°œμƒλ˜μ§€ μ•ŠλŠ”λ‹€)

- μ‹€μ œκ°’μ€ μ •μˆ˜μ΄κΈ° λ•Œλ¬Έμ— λ‹€λ₯Έ κ°’κ³Ό κ²ΉμΉ˜λŠ” 경우 이λ₯Ό 였λ₯˜λ‘œ νŒλ‹¨ν•  수 μ—†λ‹€.

interface IDAYS
{	
	int SUNDAY    = 0;	// 일
	int MONDAY    = 1;	// μ›”
	int TUESDAY   = 2;	// ν™”
	int WEDNESDAY = 3;	// 수
	int THURSDAY  = 4;	// λͺ©
	int FRIDAY    = 5;	// 금
	int SATURDAY  = 6;	// ν† 	
}

interface SCALE
{
	int DO = 0;
	int RE = 1;
	int MI = 2;
	int FA = 3;
	int SO = 4;
	int RA = 5;
	int TI = 6;
}

public class ExamEnum1 {
	
	public static void printDay(int days)
	{
		switch(days)
		{
		case IDAYS.SUNDAY :
			System.out.println("μ˜€λŠ˜μ€ μΌμš”μΌ");
			break;
			
		case IDAYS.MONDAY :
			System.out.println("μ˜€λŠ˜μ€ μ›”μš”μΌ");
			break;
			
		case IDAYS.TUESDAY :
			System.out.println("μ˜€λŠ˜μ€ ν™”μš”μΌ");
			break;
			
		case IDAYS.WEDNESDAY :
			System.out.println("μ˜€λŠ˜μ€ μˆ˜μš”μΌ");
			break;
			
		case IDAYS.THURSDAY :
			System.out.println("μ˜€λŠ˜μ€ λͺ©μš”일");
			break;
			
		case IDAYS.FRIDAY :
			System.out.println("μ˜€λŠ˜μ€ κΈˆμš”μΌ");
			break;
			
		case IDAYS.SATURDAY :
			System.out.println("μ˜€λŠ˜μ€ ν† μš”μΌ");
			break;
			
		default:
			System.out.println("잘λͺ»λœ μš”μΌμž…λ‹ˆλ‹€.");
			break;
		}
	}

	public static void main(String[] args) 
	{
		printDay(IDAYS.TUESDAY);
		printDay(SCALE.MI);
		

	}

}

 

• ν•„μš”μ„±

- 가독성을 높일 수 μžˆλ‹€.

- Data-typeμ„€μ •μœΌλ‘œ μ½”λ“œμ˜ 완전성이 μƒμŠΉλœλ‹€.

enum DAYS 
{
	SUNDAY,
	MONDAY,
	TUESDAY,
	WEDNESDAY,
	THURSDAY,
	FRIDAY,
	SATURDAY
}

enum CTYPE
{
	NORMAL,
	VIP
}

public class ExamEnum2 
{
	public static void printDay(DAYS d)
	{
		switch(d)
		{
		case SUNDAY :
			System.out.println("μ˜€λŠ˜μ€ μΌμš”μΌ");
			break;
			
		case MONDAY :
			System.out.println("μ˜€λŠ˜μ€ μ›”μš”μΌ");
			break;
			
		case TUESDAY :
			System.out.println("μ˜€λŠ˜μ€ ν™”μš”μΌ");
			break;
			
		case WEDNESDAY :
			System.out.println("μ˜€λŠ˜μ€ μˆ˜μš”μΌ");
			break;
			
		case THURSDAY :
			System.out.println("μ˜€λŠ˜μ€ λͺ©μš”일");
			break;
			
		case FRIDAY :
			System.out.println("μ˜€λŠ˜μ€ κΈˆμš”μΌ");
			break;
			
		case SATURDAY :
			System.out.println("μ˜€λŠ˜μ€ ν† μš”μΌ");
			break;
			
		default:
			System.out.println("잘λͺ»λœ μš”μΌμž…λ‹ˆλ‹€.");
			break;
		}
	}
	
	public static void main(String[] args) 
	{
		DAYS d = DAYS.SUNDAY;
		printDay(d);
		
		// 데이터 νƒ€μž…μ˜ 뢈일치둜 μ—λŸ¬ λ°œμƒ.
		// printDay(CYPTE.VIP);

	}

}

 

 


Annotation

- μ»΄νŒŒμΌλŸ¬μ—κ²Œ μ–΄λ–€ 정보λ₯Ό μ£ΌλŠ” 것이닀.

- @Override : λͺ…ν™•ν•˜κ²Œ μ»΄νŒŒμΌλŸ¬κ°€ μž¬μ •μ˜λœ λ©”μ†Œλ“œμž„.

- @Deprecated : λ¬Έμ œλ°œμƒ μ†Œμ§€κ°€ μžˆκ±°λ‚˜ ν˜Έν™˜μ„± 문제둜 인해 κ°œμ„ λœ κΈ°λŠ₯으둜 λŒ€μ²΄λ˜μ–΄ 더이상 ν•„μš”ν•˜μ§€ μ•ŠμŒ.

 

 

 


κ°€λ³€μΈμž

function(...vals);

ν•¨μˆ˜μ˜ νŒŒλΌλ―Έν„° μ•ˆμ— λ“€μ–΄κ°„ λ°μ΄ν„°μ˜ 개수만큼 λ‚΄λ¬΄μ μœΌλ‘œ 배열을 λ§Œλ“€μ–΄μ„œ 배열에 각 νŒŒλΌλ―Έν„°κ°’μ„ μ €μž₯ν•œ λ‹€μŒμ— ν•¨μˆ˜ 내뢀에 배열을 λ˜μ§€λŠ” 방식이닀.

 

public class ExamVarage 
{
	public static void printAll(String ...vars)
	{
		System.out.println("Length = " + vars.length);
		
		for(String s : vars)
		{
			System.out.println(s);
		}
	}
	
	public static void main(String[] args) 
	{
		// 마치 μ˜€λ²„λ‘œλ”©μ²˜λŸΌ 보일 수 μžˆμ§€λ§Œ
		// 사싀은 λ‚΄λΆ€μ μœΌλ‘œ λ°°μ—΄λ‘œ κ°μ‹Έμ„œ λ“€μ–΄κ°€λŠ” 것이닀.
		printAll("A");
		printAll("A", "B");
		printAll("A", "B", "C");

	}

}

 

 


Nested Class

- 클래슀 μ•ˆμ— λ“€μ–΄μžˆλŠ” 클래슀

- Static Nested Class : OuterClass의 객체화 여뢀와 μƒκ΄€μ—†μœΌλ©°, Outer Class의 μ†Œμ†μ΄κΈ° λ•Œλ¬Έμ— μ°Έμ‘°κ°€ κ°€λŠ₯ν•˜λ‹€.

- Non-Static Nested Class : innerν΄λž˜μŠ€λΌκ³ λ„ λΆˆλ¦°λ‹€.

 

 


Inner Class

• Member Inner Class

- 크래슀 μ†Œμ†μ΄λ‹€.

- Member Inner Classλ₯Ό 객체둜 μƒμ„±ν•˜λŠ” 방법은 Outerλ₯Ό μ „μ œλ‘œ newλ₯Ό 톡해 객체 생성후 Member Inner Class의 객체λ₯Ό μƒμ„±ν•œλ‹€.

 

class Outer2
{
	private int num = 0;
    
    // member inner class
    class Member
    {
    	void add(int n)
        {
        	num += n;
        }
        
        int get()
        {
        	return num;
        }
    }
}

Outer2 out1 = new Outer1();				// Outer 객체생성
Outer2.Member out1mb = out1.new Member();		// Outer ν΄λž˜μŠ€μ•ˆ Member 객체 생성

out1mb.add(1);
System.out.printnln(out1mb.get());

 

• Local Inner Class

- Method μ†Œμ†μ΄λ‹€.

 

 

λ°˜μ‘ν˜•

λŒ“κΈ€