Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[29] 230208 Java Ch. 5 ์ฐธ์กฐ ํƒ€์ž…: 3. ์—ด๊ฑฐ ํƒ€์ž… [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 29์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[29] 230208 Java Ch. 5 ์ฐธ์กฐ ํƒ€์ž…: 3. ์—ด๊ฑฐ ํƒ€์ž… [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 29์ผ]

yjyuwisely 2023. 2. 8. 12:46

230208 Wed 29th class

Ch. 5 ์ฐธ์กฐ ํƒ€์ž…
Ch. 6 ํด๋ž˜์Šค
(์ฃผ์ œ๋ณ„๋กœ ๋ฌถ๊ธฐ ์œ„ํ•ด ์œ—๊ธ€๋กœ ๊ธ€ ๋‚ด์šฉ ์˜ฎ๊น€.) 
์ง„๋„: p. 203 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”

๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ–ˆ๋‹ค. ‘๋ฌด์—‡์„’ ‘์–ด๋–ป๊ฒŒ’ ํ•™์Šตํ•ด์•ผ ํ• ์ง€ ์กฐ์ฐจ ๋ชจ๋ฅด๋Š” ์ž…๋ฌธ์ž์˜ ๋ง‰์—ฐํ•œ ๋งˆ์Œ์„ ์‚ดํŽด, ๊ณผ์™ธ ์„ ์ƒ๋‹˜์ด

www.aladin.co.kr

 

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค. 


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

2023.01.17 - [1. Revision 2023/Javascript] - [15] 230117 Ch. 9 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด: ๊ฐ์ฒด, ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ, ํ”„๋กœํ† ํƒ€์ž…, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ๊ฐ์ฒด, Array ๊ฐ์ฒด, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ


์ƒ์„ฑ์ž: ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ
์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”(๋ณ€์ˆ˜์— ์ œ์ผ ์ฒ˜์Œ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’ ex) var a = 10;) ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.


- ๋ฉ”์„œ๋“œ ์˜์—ญ(Method Area)์—๋Š” ์ •์  ํ•„๋“œ, ์ƒ์ˆ˜, ๋ฉ”์„œ๋“œ ์ฝ”๋“œ, ์ƒ์„ฑ์ž ์ฝ”๋“œ๊ฐ€ ์œ„์น˜ํ•˜๋ฉฐ, (๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.) 
ํž™ ์˜์—ญ(Heap Area)์—๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
- ์Šคํƒ ์˜์—ญ(Stack)์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.


๋ณ€์ˆ˜
ํƒ€์ž… ๋ณ€์ˆ˜๋ช…
int a;

๋ฐฐ์—ด
ํƒ€์ž… ๋ฐฐ์—ด๋ช…
int[] a;

- ๊ธฐ๋ณธ ํƒ€์ž… 8๊ฐœ: byte, short, char, int, long
   float, double
   boolean
- ์ฐธ์กฐ ํƒ€์ž…(๋‚˜๋จธ์ง€)
- ๋ฐฐ์—ด ํƒ€์ž…: int[] a;
- ์—ด๊ฑฐ ํƒ€์ž…: enum
- ํด๋ž˜์Šค
- ์ธํ„ฐํŽ˜์ด์Šค 

๋ณ€์ˆ˜ ์„ ์–ธ: int a;
๋ฐฐ์—ด ์„ ์–ธ: int[] a;
์—ด๊ฑฐ ์„ ์–ธ: public enum a(์—ด๊ฑฐ ์ด๋ฆ„);


Ch. 5 ์ž๋ฐ” ์ฐธ์กฐ ํƒ€์ž…

์ฑ…์˜ ๋ชฉ์ฐจ
05-1 ์ฐธ์กฐ ํƒ€์ž…๊ณผ ์ฐธ์กฐ ๋ณ€์ˆ˜
05-2 ๋ฐฐ์—ด
05-3 ์—ด๊ฑฐ ํƒ€์ž…

5.3 ์—ด๊ฑฐ ํƒ€์ž… p. 203

enumerate: (transitive) to mention separately or in order; name one by one; list
์—ด๊ฑฐ ํƒ€์ž…: ์—ด๊ฑฐ ํƒ€์ž…์€ ๋ช‡ ๊ฐ€์ง€๋กœ ์ œํ•œ๋œ ์ƒ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ํƒ€์ž…์„ ๋งํ•œ๋‹ค. ํ•œ์ •๋œ ๊ฐ’๋งŒ์„ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•์ด๋‹ค.

public enum ์—ด๊ฑฐํƒ€์ž…์ด๋ฆ„ {...}

Enums

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters:

An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden).
An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

์ฐธ๊ณ : https://www.w3schools.com/java/java_enums.asp


1) ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ p. 204

enum ํƒ€์ž… {์ƒ์ˆ˜, ์ƒ์ˆ˜, ...} ํ˜•ํƒœ๋กœ ์—ด๊ฑฐ ํƒ€์ž…์„ ์„ ์–ธํ•œ๋‹ค. 
ex) enum Week { MONDAY, ... , SUNDAY} 

public enum Week { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
//Week: ์—ด๊ฑฐ ํƒ€์ž… ์ด๋ฆ„
//{...}์˜ ๊ฐ’: ์—ด๊ฑฐ ์ƒ์ˆ˜

์—ด๊ฑฐ ์ƒ์ˆ˜: ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ ๋•Œ ์ฃผ์–ด์ง„ ์ƒ์ˆ˜๋ฅผ ๋งํ•˜๋ฉฐ ํƒ€์ž….์ƒ์ˆ˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
ex) Week ์—ด๊ฑฐ ํƒ€์ž…์˜ ์ƒ์ˆ˜ MONDAY๋Š” Week.MONDAY๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์ƒ์ˆ˜: ๊ฐ’์„ ํ•œ ๋ฒˆ ์ €์žฅํ•˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๋ณ€์ˆ˜. ์ƒ์ˆ˜์˜ ์ด๋ฆ„์€ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ด๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ ํ˜ผํ•ฉ๋œ ์ด๋ฆ„์ด๋ผ๋ฉด _๋กœ ๋‹จ์–ด๋“ค์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค. 
์ƒ์ˆ˜๋Š” ํ•œ ๋ฒˆ ์ •ํ•ด์ง„ ๊ฐ’์€ ๋‹ค์‹œ ๋ชป ๋ฐ”๊พผ๋‹ค. 
public enum LoginResult { LOGIN_SUCCESS, LOGIN_FAILED }

์—ด๊ฑฐ ์ƒ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋  ๊ฒฝ์šฐ์—๋Š” ๋‹จ์–ด ์‚ฌ์ด๋ฅผ ๋ฐ‘์ค„(_)๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ด๋‹ค. 

File > New > Other์—์„œ enum ์„ ํƒ ๊ฐ€๋Šฅํ•˜๋‹ค. 

์˜ˆ์ œ) p. 205
๋ฌธ์ž, ์ˆซ์ž ์ƒ๊ด€ ์—†๋‹ค.

public enum Week {
	MONDAY,
	TUESDAY,
	WEDNESDAY,
	THURSDAY,
	FRIDAY,
	SATERDAY,
	SUNDAY
}

2) ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜ p. 206

์—ด๊ฑฐ ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜์—๋Š” ์—ด๊ฑฐ ์ƒ์ˆ˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋Œ€์ž…๋œ๋‹ค.
ex) Week today = Week.FRIDAY; 
today๊ฐ€ ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜์ด๋‹ค.

์—ด๊ฑฐ ํƒ€์ž…๋„ ํ•˜๋‚˜์˜ ํƒ€์ž…์ด๋ฏ€๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์—ด๊ฑฐํƒ€์ž… ๋ณ€์ˆ˜;
Week today;
Week reservationDay;

์—ด๊ฑฐ ์ƒ์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  ๋ฐ˜๋“œ์‹œ '์—ด๊ฑฐ ํƒ€์ž….์—ด๊ฑฐ์ƒ์ˆ˜' ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

Week today = Week.SUNDAY;

์—ด๊ฑฐ ํƒ€์ž…๋„ ์ฐธ์กฐ ํƒ€์ž…์ด๋ฏ€๋กœ null ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

Week birthday = null;

 

์—ด๊ฑฐ ์ƒ์ˆ˜๋Š” ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋œ๋‹ค. 
ex) ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜ Week์˜ ๊ฒฝ์šฐ MONDAY๋ถ€ํ„ฐ SUNDAY๊นŒ์ง€์˜ ์—ด๊ฑฐ ์ƒ์ˆ˜๋Š” ์ด 7๊ฐœ์˜ WEEK ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋œ๋‹ค. 
๊ทธ๋ฆฌ๊ณ  ๋ฉ”์„œ๋“œ ์˜์—ญ์— ์ƒ์„ฑ๋œ ์—ด๊ฑฐ ์ƒ์ˆ˜๊ฐ€ ํ•ด๋‹น Week ๊ฐ์ฒด๋ฅผ ๊ฐ๊ฐ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค. 

Week today = Week.Sunday;

์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜ today๋Š” ์Šคํƒ ์˜์—ญ(๋ณ€์ˆ˜ ์ƒ์„ฑ)์— ์ƒ์„ฑ๋œ๋‹ค. today์— ์ €์žฅ๋˜๋Š” ๊ฐ’์€ Week.SUNDAY ์—ด๊ฑฐ์ƒ์ˆ˜๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด์˜ ๋ฒˆ์ง€์ด๋‹ค. 
๋”ฐ๋ผ์„œ ์—ด๊ฑฐ ์ƒ์ˆ˜ Week.SUNDAY์™€ today ๋ณ€์ˆ˜๋Š” ์„œ๋กœ ๊ฐ™์€ Week ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.
๊ทธ๋ž˜์„œ today ๋ณ€์ˆ˜์™€ Week.SUNDAY ์ƒ์ˆ˜์˜ == ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋Š” true๊ฐ€ ๋œ๋‹ค.

today == Week.SUNDAY; //true

week1๊ณผ week2๋Š” ๋ชจ๋‘ Week.SATURDAY ์ƒ์ˆ˜๋ผ๋Š” ๋™์ผํ•œ Week ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋ฏ€๋กœ week1๊ณผ week2์˜ == ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ€ true๊ฐ€ ๋œ๋‹ค.

Week week1 = Week.SATURDAY;
Week week1 = Week.SATURDAY;
System.out.println( week1 == week2 ); //true

์ž๋ฐ”๋Š” ์ปดํ“จํ„ฐ์˜ ๋‚ ์งœ ๋ฐ ์š”์ผ, ์‹œ๊ฐ„์„ ์–ป๊ธฐ ์œ„ํ•ด Caledar ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 
Calendar ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  Calendar.getInstance() ๋ฉ”์„œ๋“œ๋กœ Calendar ๊ฐ์ฒด๋ฅผ ์–ป๋Š”๋‹ค.

Calendar now = Calendar.getInstance();



get() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ, ์›”, ์ผ, ์š”์ผ, ์‹œ๊ฐ„, ๋ถ„, ์ดˆ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. 

int year = now.get(Calendar.YEAR); //์—ฐ
int month = now.get(Calendar.MONTH) + 1; //์›”(1~12)
int day = now.get(Calendar.DAY_OF_MONTH); //์ผ
int week = now.get(Calendar.DAY_OF_WEEK); //์š”์ผ(1~7)
int hour = now.get(Calendar.HOUR); //์‹œ๊ฐ„
int minute = now.get(Calendar.MINUTE); //๋ถ„
int second = now.get(Calendar.SECOND); //์ดˆ

 

์˜ˆ์ œ) ์—ด๊ฑฐ ํƒ€์ž…๊ณผ ์—ด๊ฑฐ ์ƒ์ˆ˜ p. 209

public class P208EnumWeekExample {
	public static void main(String[] args) {
		//int    a = 10;
		Week today = null; //์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜ ์„ ์–ธ
		
		//int     a  = 10;
		Calendar cal = Calendar.getInstance();//Calendar ๊ฐ์ฒด๋ฅผ ์–ป๋Š”๋‹ค.
		int week = cal.get(Calendar.DAY_OF_WEEK); //์ผ(1)~ํ† (7)๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
		
		switch(week) {
		case 1:
			today = Week.SUNDAY; break;
		case 2:
			today = Week.MONDAY; break;
		case 3:
			today = Week.TUESDAY; break;
		case 4:
			today = Week.WEDNESDAY; break;
		case 5:
			today = Week.THURSDAY; break;
		case 6:
			today = Week.FRIDAY; break;
		case 7:
			today = Week.SATERDAY; break;
		}
		
		System.out.println("์˜ค๋Š˜ ์š”์ผ: " + today);
		
		if(today==Week.SUNDAY) {
			System.out.println("์˜ค๋Š˜ ์š”์ผ: " + today);
		}else {
			System.out.println("์—ด์‹ฌํžˆ ์ž๋ฐ” ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค.:)");
		}
	}
}

๊ฒฐ๊ณผ)

์˜ค๋Š˜ ์š”์ผ: WEDNESDAY
์—ด์‹ฌํžˆ ์ž๋ฐ” ๊ณต๋ถ€ํ•ฉ๋‹ˆ๋‹ค.:)

๋งˆ๋ฌด๋ฆฌ p. 210

4๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ

1) ์—ด๊ฑฐ ํƒ€์ž…: ์—ด๊ฑฐ ํƒ€์ž…์€ ๋ช‡ ๊ฐ€์ง€๋กœ ์ œํ•œ๋œ ์ƒ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ํƒ€์ž…์„ ๋งํ•œ๋‹ค.

2) ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ: enum ํƒ€์ž… {์ƒ์ˆ˜, ์ƒ์ˆ˜, ...} ํ˜•ํƒœ๋กœ ์—ด๊ฑฐ ํƒ€์ž…์„ ์„ ์–ธํ•œ๋‹ค. 
ex) enum Week { MONDAY, ... , SUNDAY} 

3) ์—ด๊ฑฐ ์ƒ์ˆ˜: ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ ๋•Œ ์ฃผ์–ด์ง„ ์ƒ์ˆ˜๋ฅผ ๋งํ•˜๋ฉฐ ํƒ€์ž….์ƒ์ˆ˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
ex) Week ์—ด๊ฑฐ ํƒ€์ž…์˜ ์ƒ์ˆ˜ MONDAY๋Š” Week.MONDAY๋กœ ์‚ฌ์šฉํ•œ๋‹ค. 

4) ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜: ์—ด๊ฑฐ ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜์—๋Š” ์—ด๊ฑฐ ์ƒ์ˆ˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋Œ€์ž…๋œ๋‹ค.
ex) Week today = Week.FRIDAY; 
today๊ฐ€ ์—ด๊ฑฐ ํƒ€์ž… ๋ณ€์ˆ˜์ด๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments