Notice
Recent Posts
Recent Comments
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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๐ŸŒณ๐Ÿ˜Š

[31] 230210 Java Ch. 6 ํด๋ž˜์Šค: 2. ํ•„๋“œ, 3. ์ƒ์„ฑ์ž, 4. ๋ฉ”์„œ๋“œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 31์ผ] ๋ณธ๋ฌธ

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

[31] 230210 Java Ch. 6 ํด๋ž˜์Šค: 2. ํ•„๋“œ, 3. ์ƒ์„ฑ์ž, 4. ๋ฉ”์„œ๋“œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 31์ผ]

yjyuwisely 2023. 2. 10. 12:50

230210 Fri 31th class

Ch. 6 ํด๋ž˜์Šค
์ง„๋„: p. 248 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ์‹ ์šฉ๊ถŒ - ๊ต๋ณด๋ฌธ๊ณ 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ” | ํ˜ผ์ž ํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค! 1:1 ๊ณผ์™ธํ•˜๋“ฏ ๋ฐฐ์šฐ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž์Šต์„œ (JAVA 8 &11 ์ง€์›) ์ด ์ฑ…์€ ๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„

product.kyobobook.co.kr

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


Ch. 6 ํด๋ž˜์Šค

์ฑ…์˜ ๋ชฉ์ฐจ
06-1 ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
06-2 ํ•„๋“œ
06-3 ์ƒ์„ฑ์ž
06-4 ๋ฉ”์†Œ๋“œ
06-5 ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„์™€ ์ •์  ๋ฉค๋ฒ„
06-6 ํŒจํ‚ค์ง€์™€ ์ ‘๊ทผ ์ œํ•œ์ž

6.2 ํ•„๋“œ(Field) p. 224

ํ•„๋“œ: ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ(๋ฉค๋ฒ„ ๋ณ€์ˆ˜)๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ. ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ ์ „์ฒด์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜์ง€ ์•Š๋Š” ํ•œ ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ์กด์žฌํ•œ๋‹ค. 

ํ•„๋“œ๋Š” ๊ฐ์ฒด์˜ ๊ณ ์œ  ๋ฐ์ดํ„ฐ, ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ ธ์•ผ ํ•  ๋ถ€ํ’ˆ, ๊ฐ์ฒด์˜ ํ˜„์žฌ ์ƒํƒœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณณ์ด๋‹ค.

public class Car{

	//ํ•„๋“œ(์ „์—ญ๋ณ€์ˆ˜)
    
    //์•„๋ž˜๋Š” ๊ธฐ๋ณธ ํƒ€์ž…์ด๋‹ค.
    //์ œ์ž‘ํšŒ์‚ฌ(ํ˜„๋Œ€, ์‚ผ์„ฑ, ๊ธฐ์•„)
    String company;
    
    //๋ชจ๋ธ(SONATA, K5, SM)
    String model;
    
    //์ƒ‰๊น”(WHITE, BLACK, GRAY)
    String color
    
    //์ตœ๊ณ  ์†๋„(200)
    int maxSpeed;
    
    //ํ˜„์žฌ ์†๋„(60)
    int speed;
    
    //์—”์ง„ ํšŒ์ „ ์ˆ˜(1200RPM)
    int rmp;
    
    //์•„๋ž˜๋Š” ์ฐธ์กฐ ํƒ€์ž…์ด๋‹ค.
    //๋ถ€ํ’ˆ
    Body body;
    //์—”์ง„
    Engine engine;
    //ํƒ€์ด์–ด
    Tire tire;
    
    //์ƒ์„ฑ์ž
    //๋ฉ”์„œ๋“œ
}

์ฐธ๊ณ 


6.2.1 ํ•„๋“œ ์„ ์–ธ

ํด๋ž˜์Šค ์ค‘๊ด„ํ˜ธ {} ๋ธ”๋ก ์–ด๋””์„œ๋“  ์„ ์–ธํ•˜๋‚˜, ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.
ํ•„๋“œ, ์ „์—ญ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ์•ˆ ์ ์–ด๋„ ๋œ๋‹ค. (์ง€์—ญ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐ๊ฐ’ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.)

๋ถ„๋ฅ˜ ํƒ€์ž… ์ดˆ๊ธฐ๊ฐ’
๊ธฐ๋ณธ ํƒ€์ž… ์ •์ˆ˜ ํƒ€์ž… byte 0
char \u0000 (๋นˆ ๊ณต๋ฐฑ)
short 0
int 0
long 0L
์‹ค์ˆ˜ ํƒ€์ž… float 0.0F
double 0.0
๋…ผ๋ฆฌ ํƒ€์ž… boolean false
์ฐธ์กฐ ํƒ€์ž… ๋ฐฐ์—ด null
ํด๋ž˜์Šค(String ํฌํ•จ) null
์ธํ„ฐํŽ˜์ด์Šค null

6.2.2 ํ•„๋“œ ์‚ฌ์šฉ

ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค = ํ•„๋“œ๊ฐ’์„ ์ฝ๊ณ  ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…
ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์ƒ์„ฑ์ž๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋‹จ์ˆœํžˆ ํ•„๋“œ ์ด๋ฆ„์œผ๋กœ ์ฝ๊ณ  ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜์ง€๋งŒ,
ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ์šฐ์„ ์ ์œผ๋กœ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Car myCar = new Car();

myCar ๋ณ€์ˆ˜๊ฐ€ Car ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜๋ฉด ๋„ํŠธ(.) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ speed ํ•„๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. 
๋„ํŠธ(.) ์—ฐ์‚ฐ์ž๋Š” ๊ฐœ์ฒด ์ ‘๊ทผ ์—ฐ์‚ฐ์ž๋กœ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•„๋“œ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. 

์˜ˆ์ œ) Car ํด๋ž˜์Šค ํ•„๋“œ ์„ ์–ธ Car.java

public class Car {
		//ํ•„๋“œ
		//์ œ์กฐํšŒ์‚ฌ
		String company = "ํ˜„๋Œ€์ž๋™์ฐจ";
		//๋ชจ๋ธ
		String model = "๊ทธ๋žœ์ €";
		//์ฐจ์˜ ์ƒ‰๊น”
		String color = "๊ฒ€์ •";
		//์ตœ๋Œ€ ์†๋„๊ฐ’
		int maxSpeed = 350;
		//ํ˜„์žฌ ์†๋„ 
		int speed; //์œ„์™€ ๋‹ค๋ฅด๊ฒŒ ์ดˆ๊ธฐ๊ฐ’์ด ์—†๋‹ค.
	}


์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ Car ํ•„๋“œ๊ฐ’ ์ฝ๊ธฐ์™€ ๋ณ€๊ฒฝ

//main๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ
public class CarExample {
	public static void main(String[] args) {
		//new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ [ํ˜ธ์ถœ]ํ•œ๋‹ค. 
		//Carํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ฃผ์†Œ(new)๋ฅผ mycar๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
		//	 	int a = 10
		Car mycar = new Car();
		
		//Carํด๋ž˜์Šค์— ์žˆ๋Š” ํ•„๋“œ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
		System.out.println("์ œ์ž‘ํšŒ์‚ฌ : " + mycar.company);
		System.out.println("๋ชจ๋ธ๋ช… : " + mycar.company);
		System.out.println("์ƒ‰๊น” : " + mycar.company);
		System.out.println("์ตœ๊ณ ์†๋„ : " + mycar.company);
		System.out.println("ํ˜„์žฌ์†๋„ : " + mycar.company);	
	}
}


์˜ˆ์ œ2) ํ•„๋“œ ์ž๋™ ์ดˆ๊ธฐํ™”

public class FieldInitValue {
		//ํ•„๋“œ
		byte byteField;//ํƒ€์ž…์ด byte๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0
		short shortField; //ํƒ€์ž…์ด short์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0
		int intField; //ํƒ€์ž…์ด int์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0
		long longField; //ํƒ€์ž…์ด long์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0
		
		boolean booleanField; //ํƒ€์ž…์ด boolean๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ false 
		char charField; //ํƒ€์ž…์ด char์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ ๋นˆ ๊ณต๋ฐฑ
		
		float floatField; //ํƒ€์ž…์ด float๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0.0
		double doubleField; //ํƒ€์ž…์ด double์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ 0.0 
		
		int[] arrField; //ํƒ€์ž…์ด ๋ฐฐ์—ดํƒ€์ž…์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ null
		String referenceField; //ํƒ€์ž…์ด String์ด๋ฉด, ๊ธฐ๋ณธ๊ฐ’์€ null
}

ํ•„๋“œ๊ฐ’ ์ถœ๋ ฅ
FieldInitValue fiv new FieldInitValue();

public class FieldInitValueExample {
	public static void main(String[] args) {
		FieldInitValue fiv = new FieldInitValue();
		
		System.out.println("byteField: " + fiv.byteField);
		System.out.println("shortField: " + fiv.shortField);
		System.out.println("intField: " + fiv.intField);
		System.out.println("longField: " + fiv.byteField);
		
		System.out.println("booleanField: " + fiv.booleanField);
		System.out.println("charField: " + fiv.charField);
		
		System.out.println("floatField: " + fiv.floatField);
		System.out.println("doubleField: " + fiv.doubleField);
		
		System.out.println("arrField: " + fiv.arrField);
		System.out.println("referenceField: " + fiv.referenceField);
	}
}

๊ฒฐ๊ณผ)

byteField: 0
shortField: 0
intField: 0
longField: 0
booleanField: false
charField: 
floatField: 0.0
doubleField: 0.0
arrField: null
referenceField: null

 

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

1) ํ•„๋“œ ์„ ์–ธ: ํด๋ž˜์Šค ์ค‘๊ด„ํ˜ธ {} ๋ธ”๋ก ์–ด๋””์„œ๋“  ์„ ์–ธํ•˜๋‚˜, ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.
2) ํ•„๋“œ ์‚ฌ์šฉ: ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ”๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‚˜, ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Q2
์ฝ”๋“œ)

public class Member {
		//์ด๋ฆ„("์ •์ž๋ฐ”")
		String name;
		//์•„์ด๋””("abcd")
		String id;
		//ํŒจ์Šค์›Œ๋“œ("abcd1234!!")
		String password;
		//๋‚˜์ด(30)
		int age;
}

Q3
์ฝ”๋“œ) Member member new Member();

public class MemberExample {
	public static void main(String[] args) {
		//Member ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
		Member member = new Member();
		member.name = "์ตœํ•˜์–€";//name ํ•„๋“œ๊ฐ’ ๋ณ€๊ฒฝ
		member.age = 23;//age ํ•„๋“œ๊ฐ’ ๋ณ€๊ฒฝ
	}
}

6.3 ์ƒ์„ฑ์ž p. 232

์ƒ์„ฑ์ž: new ํ‚ค์›Œ๋“œ๋กœ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜์–ด ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.
(๊ฐ์ฒด ์ƒ์„ฑ: Car mycar = new Car();)

๊ฐ์ฒด ์ดˆ๊ธฐํ™”: 
ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ํ•„๋“œ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,
๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ฐธ๊ณ 

์ž๋ฐ”์—์„œ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž(constructor)๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
์ž๋ฐ”์—์„œ ์ƒ์„ฑ์ž(constructor)์˜ ์ด๋ฆ„์€ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰, Car ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„์€ Car๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
 
์ด๋Ÿฌํ•œ ์ƒ์„ฑ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
 
1. ์ƒ์„ฑ์ž๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์ง€๋งŒ, ๋ฐ˜ํ™˜ ํƒ€์ž…์„ voidํ˜•์œผ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
2. ์ƒ์„ฑ์ž๋Š” ์ดˆ๊ธฐํ™”๋ฅผ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
3. ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•  ๊ฒฝ์šฐ์—๋Š” ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    ์ฆ‰, ์ƒ์„ฑ์ž๋„ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ, ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ : http://www.tcpschool.com/java/java_methodConstructor_constructor

์ฐธ๊ณ 


6.3.1 ๊ธฐ๋ณธ ์ƒ์„ฑ์ž

๋ชจ๋“  ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž๊ฐ€ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•˜๋ฉฐ, ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜ ์ด์ƒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. 
์šฐ๋ฆฌ๊ฐ€ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ƒ์„ฑ์ž ์„ ์–ธ์„ ์ƒ๋žตํ–ˆ๋‹ค๋ฉด ํด๋ž˜์Šค ์„ ์–ธ ์‹œ ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜๋Š” ์ƒ์„ฑ์ž์ด๋‹ค. (์ƒ์„ฑ์ž๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.) ์–ธ๊ธ‰ ์•ˆ ํ•˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์“ด๋‹ค. 

ํด๋ž˜์Šค๊ฐ€ public class๋กœ ์„ ์–ธ๋˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž์—์„œ๋„ public์ด ๋ถ™์ง€๋งŒ,
ํด๋ž˜์Šค๊ฐ€ public ์—†์ด class๋กœ๋งŒ ์„ ์–ธ๋˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž์—๋„ public์ด ๋ถ™์ง€ ์•Š๋Š”๋‹ค. 

ํด๋ž˜์Šค์— ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ new ์—ฐ์‚ฐ์ž ๋’ค์— ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Car myCar = new Car();

Car(): ๊ธฐ๋ณธ ์ƒ์„ฑ์ž

javascript์˜ ๊ฒฝ์šฐ
ํ•จ์ˆ˜ ์„ ์–ธ
function add1(){}
function add2(a,b){}

ํ•จ์ˆ˜ ํ˜ธ์ถœ
add1(); //()์ด๋‹ˆ๊นŒ ()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
add2(10,20); //(a,b)์ด๋‹ˆ๊นŒ (10,20)์„ ํ˜ธ์ถœํ•œ๋‹ค.

6.3.2 ์ƒ์„ฑ์ž ์„ ์–ธ

ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋Š” ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.

1. ํด๋ž˜์Šค์ด๋ฆ„() { ... }                  // ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž ์„ ์–ธ
2. ํด๋ž˜์Šค์ด๋ฆ„(์ธ์ˆ˜1, ์ธ์ˆ˜2, ...) { ... } // ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž ์„ ์–ธ
public class Car {
		//๊ธฐ๋ณธ ์ƒ์„ฑ์ž
		//[public] ํด๋ž˜์Šค๋ช…(){}
		//Car(){}
		
		//๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹Œ ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธ
		//ํด๋ž˜์Šค๋ช…(ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ช…, ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋ช… ...){}
		Car(String color, int cc){	
		}		
}
public class CarExample {
	public static void main(String[] args) {
		//new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ [ํ˜ธ์ถœ]ํ•œ๋‹ค. 
		//Carํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ฃผ์†Œ(new)๋ฅผ mycar๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
		//	 	int a = 10
		Car mycar = new Car("๊ฒ€์ •",3000);
		//Car mycar = new Car();(x): ๊ธฐ๋ณธ ์ƒ์„ฑ์ž X
	}
}

6.3.3 ํ•„๋“œ ์ดˆ๊ธฐํ™” p. 235

ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ•„๋“œ๋Š” ๊ธฐ๋ณธ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ž๋™ ์„ค์ •๋œ๋‹ค.
๋งŒ์•ฝ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด 1) ํ•„๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ดˆ๊ธฐ๊ฐ’์„ ์ฃผ๋Š” ๋ฐฉ๋ฒ•๊ณผ 2) ์ƒ์„ฑ์ž์—๊ฒŒ ์ดˆ๊ธฐ๊ฐ’์„ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 1)์˜ ๊ฒฝ์šฐ ๋™์ผํ•œ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋“ค์€ ๋ชจ๋‘ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. 

์˜ˆ์ œ) ์ƒ์„ฑ์ž์—์„œ ํ•„๋“œ ์ดˆ๊ธฐํ™”
'this.ํ•„๋“œ'๋Š” this๋ผ๋Š” ์ฐธ์กฐ ๋ณ€์ˆ˜๋กœ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค. 

public class Korean {
	//ํ•„๋“œ
	//๊ตญ๊ฐ€
	String nation = "๋Œ€ํ•œ๋ฏผ๊ตญ"; //๋Œ€ํ•œ๋ฏผ๊ตญ์œผ๋กœ nationํ•„๋“œ์— ์ดˆ๊ธฐํ™”
	//์ดˆ๊ธฐํ™”(x) -> ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž์— ์˜ํ•ด์„œ "๋ฐ•์ž๋ฐ”"๋ผ๋Š” ๋ฌธ์ž์—ด์ด name ํ•„๋“œ์— ์ดˆ๊ธฐํ™”
	String name; 
	//์ดˆ๊ธฐํ™”(x) -> ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž์— ์˜ํ•ด์„œ "011225-1234567"๋ผ๋Š” ๋ฌธ์ž์—ด์ด ssn ํ•„๋“œ์— ์ดˆ๊ธฐํ™”
	String ssn;
	
	//์ž๋™ ์ถ”๊ฐ€: ์•„๋ž˜์ชฝ์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž์— ์˜ํ•ด์„œ
	//์ž๋™ ์ถ”๊ฐ€๋œ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋Š” ์—†์–ด์ง„๋‹ค.
	//Korean()
	
	//์ƒ์„ฑ์ž
	Korean(String name, String ssn){
		this.name = name;
		this.ssn = ssn;
	}
}

6.3.4 ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ (Overloading) p. 238

์™ธ๋ถ€์—์„œ ์ œ๊ณต๋˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด ์ƒ์„ฑ์ž๋„ ๋‹ค์–‘ํ™”๋  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

์˜ค๋ฒ„๋กœ๋”ฉ: ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋‹ฌ๋ฆฌํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

์˜ค๋ฒ„๋กœ๋”ฉ์˜ ์กฐ๊ฑด
- ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์ด ๊ฐ™๋‹ค.
- ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค.
- ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค. 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ์šฐ)

//๋ฉ”์„œ๋“œ ์„ ์–ธ
//์ •์ˆ˜+์ •์ˆ˜
int add(int a, int b){
	int result = a + b;
    return result;
}
//์ •์ˆ˜+์‹ค์ˆ˜
double add2(int a, double b){
	double result = a + b;
    return result;
}
//์‹ค์ˆ˜+์ •์ˆ˜
double add3(double a, int b){
	double result = a + b;
    return result;
}
//์‹ค์ˆ˜+์‹ค์ˆ˜
double add4(double a, double b){
	double result = a + b;
    return result;
}
//๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
add1(1,2);
add2(1,2.5);
add3(1.5,2);
add4(1.5,2.5);

์ž๋ฐ”์˜ ๊ฒฝ์šฐ)

//๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
add(1,2);
add(1,2.5);
add(1.5,2);
add(1.5,2.5);

 

์˜ˆ์ œ) ์ƒ์„ฑ์ž์˜ ์˜ค๋ฒ„๋กœ๋”ฉ p. 239

public class Car {
		//ํ•„๋“œ
		//์ œ์กฐํšŒ์‚ฌ
		String company = "ํ˜„๋Œ€์ž๋™์ฐจ";
		//๋ชจ๋ธ
		String model; //= "๊ทธ๋žœ์ €";
		//์ฐจ์˜ ์ƒ‰๊น”
		String color; //= "๊ฒ€์ •";
		//์ตœ๋Œ€ ์†๋„๊ฐ’
		int maxSpeed; //= 350;

		//์ƒ์„ฑ์ž ์„ ์–ธ
		Car(){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž - ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
			
		}
		
		Car(String model){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 1๊ฐœ์ธ ์ƒ์„ฑ์ž
			this.model = model;
		}
		
		Car(String model, String color){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž - ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํƒ€์ž…์ด ๊ฐ™์Œ(String, String)
			this.model = model;
			this.color = color;
		}
		
		Car(String model, int speed){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž - ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํƒ€์ž…์ด ๋‹ค๋ฆ„(String, int)
			this.model = model;
			this.speed = speed;
		}
		
		Car(String model, String color, int maxSpeed){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž
			this.model = model;
			this.color = color;
			this.maxSpeed = maxSpeed;
		}
}

์˜ˆ์ œ) ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ์ƒ์„ฑ์ž ์„ ํƒ

public class CarExample {
	public static void main(String[] args) {
		//new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ [ํ˜ธ์ถœ]ํ•œ๋‹ค. 
		//Carํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ฃผ์†Œ(new)๋ฅผ mycar๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
		//	 	int a = 10
		//Car mycar = new Car("๊ฒ€์ •",3000);
		//Car mycar = new Car();(x): ๊ธฐ๋ณธ ์ƒ์„ฑ์ž X
		
		//Carํด๋ž˜์Šค์— ์žˆ๋Š” ํ•„๋“œ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
		//System.out.println("์ œ์ž‘ํšŒ์‚ฌ : " + mycar.company);
		//System.out.println("๋ชจ๋ธ๋ช… : " + mycar.company);
		//System.out.println("์ƒ‰๊น” : " + mycar.company);
		//System.out.println("์ตœ๊ณ ์†๋„ : " + mycar.company);
		//System.out.println("ํ˜„์žฌ์†๋„ : " + mycar.company);	
		
		//Carํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ฃผ์†Œ(new)๋ฅผ mycar๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
		//				๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž ํ˜ธ์ถœ - ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
		Car car1 = new Car();
		System.out.println(car1.company);
		System.out.println();
		
		Car car2 = new Car("์ž๊ฐ€์šฉ");
		System.out.println(car2.company);
		System.out.println(car2.model);
		System.out.println();
		
		Car car3 = new Car("์ž๊ฐ€์šฉ", "๋นจ๊ฐ•");
		System.out.println(car3.company);
		System.out.println(car3.model);
		System.out.println(car3.color);
		System.out.println();
		
		Car car31 = new Car("์ž๊ฐ€์šฉ", 200);
		System.out.println(car31.company);
		System.out.println(car31.model);
		System.out.println(car31.speed);
		
		Car car4 = new Car("์ž๊ฐ€์šฉ", "๋นจ๊ฐ•", 200);
		System.out.println(car4.company);
		System.out.println(car4.model);
		System.out.println(car3.color);
		System.out.println(car4.maxSpeed);
	}
}

6.3.5 ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ: this() p. 241

this = ๋‚˜
this(): ๊ฐ์ฒด ์ž์‹ ์˜ ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ ์ƒ์„ฑ์ž ๊ฐ„์˜ ์ค‘๋ณต๋œ ์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ•„๋“œ ์ดˆ๊ธฐํ™” ๋‚ด์šฉ์€ ํ•œ ์ƒ์„ฑ์ž์—๊ฒŒ๋งŒ ์ง‘์ค‘์ ์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์ƒ์„ฑ์ž๋Š” ์ดˆ๊ธฐํ™” ๋‚ด์šฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

this()๋Š” ์ž์‹ ์ด ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ๋กœ ๋ฐ˜๋“œ์‹œ ์ƒ์„ฑ์ž์˜ ์ฒซ ์ค„์—์„œ๋งŒ ํ—ˆ์šฉ๋œ๋‹ค. this() ๋‹ค์Œ์—๋Š” ์ถ”๊ฐ€์ ์ธ ์‹คํ–‰๋ฌธ๋“ค์ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

this() ๋ฉ”์„œ๋“œ๋Š” ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
this() ๋ฉ”์„œ๋“œ์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด, ์ƒ์„ฑ์ž ์ค‘์—์„œ ๋ฉ”์„œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜(๋ฉ”์„œ๋“œ ์ด๋ฆ„๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ์˜ ์กฐํ•ฉ)๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ์ฐพ์•„ ํ˜ธ์ถœํ•ด ์ค๋‹ˆ๋‹ค.

์˜ˆ์ œ 1) ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ค‘๋ณต ์ฝ”๋“œ ์ค„์ด๊ธฐ 

public class Car {
		//ํ•„๋“œ
		//์ œ์กฐํšŒ์‚ฌ
		String company = "ํ˜„๋Œ€์ž๋™์ฐจ";
		//๋ชจ๋ธ
		String model; //= "๊ทธ๋žœ์ €";
		//์ฐจ์˜ ์ƒ‰๊น”
		String color; //= "๊ฒ€์ •";
		//์ตœ๋Œ€ ์†๋„๊ฐ’
		int maxSpeed; //= 350;
		//ํ˜„์žฌ ์†๋„ 
		int speed; //์œ„์™€ ๋‹ค๋ฅด๊ฒŒ ์ดˆ๊ธฐ๊ฐ’์ด ์—†๋‹ค.
		
		//์ƒ์„ฑ์ž ์„ ์–ธ
		Car(){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž - ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
			
		}
		
		Car(String model){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 1๊ฐœ์ธ ์ƒ์„ฑ์ž
			//this.model = model;
			this(model,"์€์ƒ‰",250);
		}
		
		Car(String model, String color){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž - ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํƒ€์ž…์ด ๊ฐ™์Œ(String, String)
			this.model = model;
			this.color = color;
		}
		
		Car(String model, int speed){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž - ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํƒ€์ž…์ด ๋‹ค๋ฆ„(String, int)
			this.model = model;
			this.speed = speed;
		}
		
		Car(String model, String color, int maxSpeed){//๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž
			this.model = model;
			this.color = color;
			this.maxSpeed = maxSpeed;
		}
}

๋งˆ๋ฌด๋ฆฌ 
6๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ
1) ๊ธฐ๋ณธ ์ƒ์„ฑ์ž: 
ํด๋ž˜์Šค ์„ ์–ธ ์‹œ ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜๋Š” ์ƒ์„ฑ์ž์ด๋‹ค.


2) ์ƒ์„ฑ์ž ์„ ์–ธ: ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•˜๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋Š” ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.

3) ๋งค๊ฐœ ๋ณ€์ˆ˜:
 ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์‹œ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๊ธฐ ์œ„ํ•ด ์„ ์–ธ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งํ•œ๋‹ค.


4) ๊ฐ์ฒด ์ดˆ๊ธฐํ™”:
 ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์ค€๋น„ํ•˜๋Š” ๊ณผ์ •์œผ๋กœ

ํ•„๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ํ•„๋“œ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,
๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‚ด์šฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. 

5) ์˜ค๋ฒ„๋กœ๋”ฉ:
 ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋‹ฌ๋ฆฌํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.


6) this(): ๊ฐ์ฒด ์ž์‹ ์˜ ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 



6.4 ๋ฉ”์„œ๋“œ p. 247

     ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์˜ ๋™์ž‘์— ํ•ด๋‹นํ•˜๋Š” ์ค‘๊ด„ํ˜ธ {} ๋ธ”๋ก์ด๋‹ค. ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก ์ด๋ฆ„์ด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด๋ฉฐ, ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ค‘๊ด„ํ˜ธ ๋ธ”๋ก์— ์žˆ๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋“ค์ด ์ผ๊ด„์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค. 


6.4.1 ๋ฉ”์„œ๋“œ ์„ ์–ธ

๋ฉ”์„œ๋“œ ์„ ์–ธ์€ ์„ ์–ธ๋ถ€์™€ ์‹คํ–‰ ๋ธ”๋ก์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. 

๋ฆฌํ„ดํƒ€์ž… ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ([๋งค๊ฐœ๋ณ€์ˆ˜์„ ์–ธ, ...]_{

	์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ณณ //๋ฉ”์„œ๋“œ ์‹คํ–‰ ๋ธ”๋ก
    
}

๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€ (method signature): ๋ฆฌํ„ด ํƒ€์ž…, ๋ฉ”์„œ๋“œ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜ ์„ ์–ธ
     - ๋ฆฌํ„ด ํƒ€์ž…: ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒฐ๊ณผ ํƒ€์ž… ํ‘œ์‹œ
     - ๋ฉ”์„œ๋“œ ์ด๋ฆ„
     - ๋งค๊ฐœ๋ณ€์ˆ˜์„ ์–ธ: ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
๋ฉ”์„œ๋“œ ์‹คํ–‰ ๋ธ”๋ก: ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

์˜ˆ์‹œ)

public class Calculator {
	//ํ•„๋“œ
	
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	void powerOn() {
		System.out.println("์ „์›์„ ์ผญ๋‹ˆ๋‹ค.");
	}
	
	int plus(int x, int y) {
		int result = x + y;
		return result;
	}
	
	double divide(int x, int y) {
		double result = (double)x / (double)y;
		return result;
	}
	
	void powerOff() {
		System.out.println("์ „์›์„ ๋•๋‹ˆ๋‹ค.");
	}
}

1) ๋ฆฌํ„ด ํƒ€์ž…

๋ฆฌํ„ด ํƒ€์ž… = ๋ฆฌํ„ด๊ฐ’์˜ ํƒ€์ž…
๋ฆฌํ„ด๊ฐ’ = ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ›„์˜ ๊ฒฐ๊ด๊ฐ’ 

๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฆฌํ„ด ํƒ€์ž…์ด ์„ ์–ธ๋ถ€์— ๋ช…์‹œ๋˜์–ด์•ผ ํ•œ๋‹ค.
๋ฆฌํ„ด๊ฐ’์ด ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ์— ๋”ฐ๋ผ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค.

powerOn(); // ๋ฆฌํ„ด๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜์— ์ €์žฅํ•  ๋‚ด์šฉ์ด ์—†๋‹ค. 
double result = divide(10, 20); //๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•ด ๋ฆฌํ„ด ํƒ€์ž…์ธ double ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•œ๋‹ค.
int result = divide(10, 20); //์ปดํŒŒ์ผ ์—๋Ÿฌ

๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•ด ๋ฆฌํ„ด ํƒ€์ž…์ธ double ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•œ๋‹ค.
๋งŒ์•ฝ result ๋ณ€์ˆ˜๋ฅผ int ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด double ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋ฆฌํ„ด๊ฐ’์ด ์ค‘์š”ํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ ์‹คํ–‰์ด ์ค‘์š”ํ•  ๊ฒฝ์šฐ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

divide(10, 20);

2) ๋ฉ”์„œ๋“œ ์ด๋ฆ„

- ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์•ˆ ๋˜๊ณ , $, _๋ฅผ ์ œ์™ธํ•œ ํŠน์ˆ˜ ๋ฌธ์ž ์‚ฌ์šฉ X
- ๊ด€๋ก€์ ์œผ๋กœ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ ์†Œ๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค.
- ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๊ฐ€ ํ˜ผํ•ฉ๋œ ์ด๋ฆ„์ด๋ผ๋ฉด ๋’ค์ด์–ด ์˜ค๋Š” ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค.

์ž˜ ์ž‘์„ฑ๋œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์˜ ์˜ˆ์‹œ (๋ฉ”์„œ๋“œ๊ฐ€ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค. :))

void run(){...}
void startEngine(){...}
String getName(){...}
int[] getScores(){...}

3) ๋งค๊ฐœ ๋ณ€์ˆ˜ ์„ ์–ธ p. 250

๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๊ณ , ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.
๋งค๊ฐœ๊ฐ’์€ ๋ฐ˜๋“œ์‹œ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์— ๋ถ€ํ•ฉ๋˜๋Š” ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค. 

void: ๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ void๋ฅผ ๊ธฐ์ˆ ํ•ด์•ผ ํ•œ๋‹ค.
์˜ˆ์ œ) ๋ฉ”์„œ๋“œ ์„ ์–ธ 

public class Calculator {
	//ํ•„๋“œ
	
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	void powerOn() {
		System.out.println("์ „์›์„ ์ผญ๋‹ˆ๋‹ค.");
	}
	
	int plus(int x, int y) {
		int result = x + y;
		return result;
	}
	
	double divide(int x, int y) {
		double result = (double)x / (double)y;
		return result;
	}
	
	void powerOff() {
		System.out.println("์ „์›์„ ๋•๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ) ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

public class CalculatorExample {
	public static void main(String[] args) {
		Calculator myCalc = new Calculator();
		//Calculator ํด๋ž˜์Šค์˜ powerOn ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCalc.powerOn();
		
		//Calculator ํด๋ž˜์Šค์˜ plus ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		int result1 = myCalc.plus(5, 6);
		System.out.println(result1);
		
		byte x = 10;
		byte y = 4;
		
		//Calculator ํด๋ž˜์Šค์˜  divide ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		double result2 = myCalc.divide(x, y);
		System.out.print(result2);
		
		//Calculator ํด๋ž˜์Šค์˜ powerOff ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCalc.powerOff();
	}
}

4) ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๊ฒฝ์šฐ

ex) ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธ 

ํ•ด๊ฒฐ์ฑ…์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

int sum1(int[] values){}

//sum1() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฐฐ์—ด์„ ๋„˜๊ฒจ์คŒ์œผ๋กœ์จ ๋ฐฐ์—ด์˜ ํ•ญ๋ชฉ ๊ฐ’๋“ค์„ ๋ชจ๋‘ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
//๋ฐฐ์—ด์˜ ํ•ญ๋ชฉ ์ˆ˜๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๊ฒฐ์ •๋œ๋‹ค.
int[] values = {1, 2, 3};
int result = sum1(values);
int result = sum1(new int[] {1, 2, 3, 4, 5});

๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•œ ์ ์ด ์žˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ฐ’์˜ ๋ชฉ๋ก๋งŒ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

sum2() ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ...๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ๋„˜๊ฒจ์ค€ ๊ฐ’์˜ ์ˆ˜์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋˜๊ณ  ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

int sum2(int ... values){}

//...๋กœ ์„ ์–ธ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ ์‰ผํ‘œ๋กœ ๋‚˜์—ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
int result = sum2(1, 2, 3);
int result = sum2(1, 2, 3, 4, 5);

//๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์„ ์ง์ ‘ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ์ข‹๋‹ค.
int[] values = {1, 2, 3};
int result = sum2(values);
int result = sum2(new int[]{1, 2, 3, 4, 5});

์˜ˆ์ œ 1) ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๊ฒฝ์šฐ

๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰๋ฌธ์ด ์™„์ „ํžˆ ์ผ์น˜ํ•œ๋‹ค. ์„ ์–ธ ๋ฐฉ๋ฒ•๋งŒ ๋‹ค๋ฅผ ๋ฟ์ด์ง€ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ๋ฐฐ์—ด์ด๋ฏ€๋กœ ์ฒ˜๋ฆฌ ๋‚ด์šฉ์ด ๊ฐ™๋‹ค.

public class Computer {
	//๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๋•Œ
	//1. ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๋ฐฐ์—ด ์„ ์–ธ
	int sum1(int[] values) {
		int sum = 0;
		for(int i=0; i<values.length; i++) {
			sum += values[i];
		}
		return sum;
	}
	
	//2. ...
	int sum2(int ... values) {
		int sum = 0;
		for(int i=0; i<values.length; i++) {
			sum += values[i];
		}
		return sum;
	}
}

์˜ˆ์ œ 2) ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๊ฒฝ์šฐ

public class ComputerExample {
	public static void main(String[] args) {
		Computer myCom = new Computer();
		
		int[] values1 = {1, 2, 3};
		int result1 = myCom.sum1(values1);
		System.out.println("result1: "+result1);
		
		int result2 = myCom.sum1(new int[] {1, 2, 3, 4, 5});
		System.out.println("result2: "+result2);
		
		int result3 = myCom.sum2(1, 2, 3);
		System.out.println("result3: "+result3);
		
		int result4 = myCom.sum2(1, 2, 3, 4, 5);
		System.out.println("result4: "+result4);
	}
}

6.4.2 ๋ฆฌํ„ด(return)๋ฌธ p. 254

1) ๋ฆฌํ„ด๊ฐ’์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ

return ๋ฆฌํ„ด๊ฐ’;

return๋ฌธ ์ดํ›„์˜ ์‹คํ–‰๋ฌธ์€ ๊ฒฐ์ฝ” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. 


2) ๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ: void p. 255

๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ void๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
void๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ์—์„œ๋„ return๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌํ„ด๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ
๋ฉ”์„œ๋“œ ์‹คํ–‰์„ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ์—ญํ• ์ด๋‹ค.

return;
return (null): null๋กœ returnํ•œ๋‹ค.
submit์„ ํ•˜์ง€ ๋ชปํ•˜๊ณ  null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•ด๋‹น return ์•„๋ž˜๋ฅผ ์ „๋ถ€ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  return ์ค„์—์„œ ํด๋ž˜์Šค๋ฅผ ๋๋‚ด๋ฒ„๋ฆฐ๋‹ค.

return true: submit ํ•˜๊ธฐ ์ „์— ํ•จ์ˆ˜์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  ํ†ต๊ณผ๊ฐ€ ๋˜๋ฉด true(=submit true)๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

return false: ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์œผ๋ฉด submit์„ ํ•˜์ง€ ์•Š๊ณ  fasle๋กœ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ๋œป(return null๊ณผ ์•„์ฃผ ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.)์ด๋‹ค.

์ฐธ๊ณ : https://diaryofgreen.tistory.com/80

์˜ˆ์ œ 1) return๋ฌธ

public class Car {
	//ํ•„๋“œ
	int gas;
	
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	void setGas(int gas) {
		this.gas = gas; //๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋กœ ๋งค๊ฐœ๊ฐ’์„ ๋ฐ›์•„์„œ gas ํ•„๋“œ๊ฐ’์„ ๋ณ€๊ฒฝ
	}
	
	//๋ฆฌํ„ด๊ฐ’์ด boolean์ธ ๋ฉ”์„œ๋“œ๋กœ gasํ•„๋“œ๊ฐ’์ด 0์ด๋ฉด false๋ฅผ, 0์ด ์•„๋‹ˆ๋ฉด true๋ฅผ ๋ฆฌํ„ด
	boolean isLeftGas() {
		if(gas == 0) {
			System.out.println("gas๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
			return false; //false๋ฅผ ๋ฆฌํ„ด
		}
		System.out.println("gas๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.");
		return true; //true๋ฅผ ๋ฆฌํ„ด
	}
	
	//๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋กœ gas ํ•„๋“œ๊ฐ’์ด 0์ด๋ฉด return๋ฌธ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ•์ œ์ข…๋ฃŒ
	void run() {
		while(true) {
			if(gas > 0) {
				System.out.println("๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:" + gas + ")");
				gas -= 1;
			}else {
				System.out.println("๋ฉˆ์ถฅ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:" + gas + ")");
				return;
			}
		}
	}
}



์˜ˆ์ œ 2) return๋ฌธ

public class CarExample {
	public static void main(String[] args) {
		Car myCar = new Car();
		
		//Car์˜ setGas ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCar.setGas(5); 
		
		//isLeftGas๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		boolean gasState = myCar.isLeftGas();
		if(gasState){
			System.out.println("์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.");
			myCar.run(); //Car์˜ run() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		}
		
		//Car์˜ isLeftGas() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		if(myCar.isLeftGas()) {
			System.out.println("gas๋ฅผ ์ฃผ์ž…ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}else {
			System.out.println("gas๋ฅผ ์ฃผ์ž…ํ•˜์„ธ์š”.");
		}
	}
}

๊ฒฐ๊ณผ) 

gas๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ถœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:5)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:4)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:3)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:2)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:1)
๋ฉˆ์ถฅ๋‹ˆ๋‹ค.(gas์ž”๋Ÿ‰:0)
gas๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
gas๋ฅผ ์ฃผ์ž…ํ•˜์„ธ์š”.

6.4.3 ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ p. 258

๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค ๋‚ด, ์™ธ๋ถ€์˜ ํ˜ธ์ถœ์— ์˜ํ•ด ์‹คํ–‰๋œ๋‹ค.
- ํด๋ž˜์Šค ๋‚ด๋ถ€: ๋‹จ์ˆœํ•œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์œผ๋กœ ํ˜ธ์ถœ
- ํด๋ž˜์Šค ์™ธ๋ถ€: ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค ์ฐธ์กฐ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. (๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•ด์•ผ ๋ฉ”์„œ๋“œ๋„ ์กด์žฌํ•˜๋ฏ€๋กœ)

//์™ธ๋ถ€
void ์™ธ๋ถ€๋ฉ”์„œ๋“œ(){
Car car = new Car();
car.run();
car.stop();
car.sound();
}

1) ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ ํ˜ธ์ถœ

๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด '๋ฉ”์„œ๋“œ ์ด๋ฆ„(๋งค๊ฐœ๊ฐ’, ...)' ํ˜•ํƒœ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ) ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

public class Calculator {
	//๋ฉ”์„œ๋“œ ์„ ์–ธ
	//๋ฆฌํ„ดํƒ€์ž… ๋ฉ”์„œ๋“œ๋ช…([ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜])
	int plus(int x, int y) {
		int result = x + y;
		return result;
	}
	
	double avg(int x, int y) {
		double sum = plus(x, y);//plus๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		double result = sum / 2;
		return result;
	}
	
	void execute() {
		double result = avg(7, 10);//avg๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		println("์‹คํ–‰๊ฒฐ๊ณผ: " + result);//println๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
	}
	
	void println(String message) {
		System.out.println(message);
	}
}

์˜ˆ์ œ) Calculator์˜ execute() ์‹คํ–‰

public class CalculatorExample {
	public static void main(String[] args) {
		Calculator myCalc = new Calculator();
		//Calculator ํด๋ž˜์Šค์˜ powerOn ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCalc.powerOn();
		
		//Calculator ํด๋ž˜์Šค์˜ plus ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		int result1 = myCalc.plus(5, 6);
		System.out.println(result1);
		
		byte x = 10;
		byte y = 4;
		
		//Calculator ํด๋ž˜์Šค์˜  divide ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		double result2 = myCalc.divide(x, y);
		System.out.print(result2);
		
		//Calculator ํด๋ž˜์Šค์˜ powerOff ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		myCalc.powerOff();
	}
}

2) ๊ฐ์ฒด ์™ธ๋ถ€์—์„œ ํ˜ธ์ถœ

์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.
๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์— ์†Œ์†๋œ ๋ฉค๋ฒ„์ด๋ฏ€๋กœ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”์„œ๋“œ๋„ ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํด๋ž˜์Šค ์ฐธ์กฐ ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค(๋งค๊ฐœ๊ฐ’, ...);

๋„ํŠธ ์—ฐ์‚ฐ์ž๋Š” ๊ฐ์ฒด ์ ‘๊ทผ ์—ฐ์‚ฐ์ž๋กœ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•„๋“œ๋‚˜ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

์ฐธ์กฐ๋ณ€์ˆ˜.๋ฉ”์„œ๋“œ(๋งค๊ฐœ๊ฐ’, ...); //๋ฆฌํ„ด๊ฐ’์ด ์—†๊ฑฐ๋‚˜, ์žˆ์–ด๋„ ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›์ง€ ์•Š์„ ๊ฒฝ์šฐ
ํƒ€์ž… ๋ณ€์ˆ˜ = ์ฐธ์กฐ๋ณ€์ˆ˜.๋ฉ”์„œ๋“œ(๋งค๊ฐœ๊ฐ’, ...); //๋ฆฌํ„ด๊ฐ’์ด ์žˆ๊ณ , ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ

์˜ˆ์ œ 1) ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

public class Car2 {
	//ํ•„๋“œ
	int speed;
	//์ƒ์„ฑ์ž
	
	//๋ฉ”์„œ๋“œ
	int getSpeed() {
		return speed;
	}
	void keyTurnOn() {
		System.out.println("ํ‚ค๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค.");
	}
	
	void run() {
		for(int i=0; i<=50; i+=10) {
			speed = i;
			System.out.println("๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: "+speed+"km/h)");
		}
	}	
}

์˜ˆ์ œ 2) ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

public class Car2Example {
	public static void main(String[] args) {
		//๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		Car2 myCar = new Car2();
		myCar.keyTurnOn();
		myCar.run();
		int speed = myCar.getSpeed();
		System.out.println("ํ˜„์žฌ ์†๋„: "+speed+"km/h");
	}
}

๊ฒฐ๊ณผ)

ํ‚ค๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค.
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 0km/h)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 10km/h)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 20km/h)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 30km/h)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 40km/h)
๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.(์‹œ์†: 50km/h)
ํ˜„์žฌ ์†๋„: 50km/h

6.4.4 ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ (Overloading) p. 263

๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ: ํด๋ž˜์Šค ๋‚ด์— ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ
์˜ค๋ฒ„๋กœ๋”ฉ: ๋งŽ์ด ์‹ฃ๋Š” ๊ฒƒ
ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์œผ๋กœ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•œ๋‹ค.
์กฐ๊ฑด: ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…, ๊ฐœ์ˆ˜, ์ˆœ์„œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋‹ฌ๋ผ์•ผ ํ•œ๋‹ค. 

๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” ๋งค๊ฐœ๊ฐ’์„ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. 

๋งค๊ฐœ ๋ณ€์ˆ˜ ํƒ€์ž…์ด ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ž๋™ ํƒ€์ž… ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•œ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…, ๊ฐœ์ˆ˜, ์ˆœ์„œ๊ฐ€ ๋˜‘๊ฐ™์„ ๊ฒฝ์šฐ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ๋‹ค๋ฅด๋‹ค๊ณ  ํ•ด์„œ ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ์ด๋ผ๊ณ  ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ์˜ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋Š” System.out.println() ๋ฉ”์„œ๋“œ์ด๋‹ค. ํ˜ธ์ถœํ•  ๋•Œ ์ฃผ์–ด์ง„ ๋งค๊ฐœ๊ฐ’์˜ ํƒ€์ž…์— ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ค๋ฒ„๋กœ๋”ฉ๋œ println()  ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

void println(){...}
void println(boolean x){...}
void println(char x){...}
void println(char[] x){...}
void println(double x){...}
void println(float x){...}
void println(long x){...}
void println(Object x){...}
void println(String x){...}

 

์˜ˆ์ œ 1) ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ
์–ด๋–ค ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๊ฒƒ์ธ์ง€๋Š” ๋งค๊ฐœ๊ฐ’์˜ ๋ณ€์ˆ˜์— ๋‹ฌ๋ ค์žˆ๋‹ค.

public class Calculator {
	//์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด
	double areaRectangle(double width) {
		return width * width;
	}
	
	//์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด
	double areaRectangle(double width, double height) {
		return width * height;
	}
	
	//์›์˜ ๋„“์ด
	double areaRectangle(int r) {
		return r * r * Math.PI;
	}
}

์˜ˆ์ œ 2) ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ

public class CalculatorExample {
	public static void main(String[] args) {
		Calculator myCalcu = new Calculator();
	//์ •์‚ฌ๊ฐํ˜•์˜ ๋„“์ด ๊ตฌํ•˜๊ธฐ
	double result1 = myCalcu.areaRectangle(10);
	//์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด ๊ตฌํ•˜๊ธฐ
	double result2 = myCalcu.areaRectangle(10, 20);
	//์›์˜ ๋„“์ด ๊ตฌํ•˜๊ธฐ
	double result3 = myCalcu.areaRectangle(10);
	
	//๊ฒฐ๊ณผ ์ถœ๋ ฅ
	System.out.println("์ •์‚ฌ๊ฐํ˜• ๋„“์ด=" + result1);
	System.out.println("์ •์‚ฌ๊ฐํ˜• ๋„“์ด=" + result2);
	System.out.println("์›์˜ ๋„“์ด=" + result3);
	}
}

๋งˆ๋ฌด๋ฆฌ p. 267
6๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ
1) ์„ ์–ธ๋ถ€:
 ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€๋Š” ๋ฆฌํ„ด ํƒ€์ž…, ๋ฉ”์„œ๋“œ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ถ€๋ถ„์„ ๋งํ•œ๋‹ค. 

2) void: 
๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ void๋ฅผ ๊ธฐ์ˆ ํ•ด์•ผ ํ•œ๋‹ค. 

3) ๋งค๊ฐœ ๋ณ€์ˆ˜: 
๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ œ๊ณต๋˜๋Š” ๋งค๊ฐœ๊ฐ’์€ ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ์ฐจ๋ก€๋Œ€๋กœ ๋Œ€์ž…๋˜์–ด, ๋ฉ”์„œ๋“œ ๋ธ”๋ก ์‹คํ–‰ ์‹œ ์ด์šฉ๋œ๋‹ค. 

4) ๋ฆฌํ„ด๋ฌธ:
 ๋ฉ”์„œ๋“œ ์„ ์–ธ๋ถ€์— ๋ฆฌํ„ด ํƒ€์ž…์ด ์žˆ๋‹ค๋ฉด ๋ฆฌํ„ด๊ฐ’์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด return๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋ฆฌํ„ด ํƒ€์ž…์ด void๋ผ๋ฉด return๋ฌธ์€ ํ•„์š” ์—†์ง€๋งŒ, ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์ข…๋ฃŒ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

5) ํ˜ธ์ถœ:
 ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ ค๋ฉด '๋ฉ”์„œ๋“œ ์ด๋ฆ„(๋งค๊ฐœ๊ฐ’, ...)' ํ˜•ํƒœ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

6) ์˜ค๋ฒ„๋กœ๋”ฉ: 
ํด๋ž˜์Šค ๋‚ด์— ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ
์กฐ๊ฑด์€ ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํƒ€์ž…, ๊ฐœ์ˆ˜, ์ˆœ์„œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋‹ฌ๋ผ์•ผ ํ•œ๋‹ค. 


Q3 
์˜ˆ์ œ) MemberService.java

public class MemberService {
	//์ž‘์„ฑ
	//MemberService ํด๋ž˜์Šค
	//login ๋ฉ”์„œ๋“œ ์„ ์–ธ
	//id, password ์ œ๊ณต
	boolean login(String id, String password) {
		//id = "hong", password = "12345" true ๋ฆฌํ„ด
		if(id.equals("hong")&&password.equals("12345")) {
			return true;
	}else {	//๊ทธ ์™ธ false ๋ฆฌํ„ด 
		return false;
	}
	}
	//logout ๋ฉ”์„œ๋“œ ์„ ์–ธ
		//id ์ œ๊ณต
	void logout(String id) {//"๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค." ์ถœ๋ ฅ 
		System.out.println("๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
	}
}

์˜ˆ์ œ) MemberServiceExample.java

public class MemberServiceExample {
	public static void main(String[] args) {
		MemberService memberService = new MemberService();
		boolean result = memberService.login("hong", "12345");
		if(result) {
			System.out.println("๋กœ๊ทธ์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
			memberService.logout("hong");
		}else {
			System.out.println("id ๋˜๋Š” password๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		}
	}
}

๊ฒฐ๊ณผ)

๋กœ๊ทธ์ธ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Q4) 

์˜ˆ์ œ) Printer.java

public class Printer {
	//์ž‘์„ฑ ์œ„์น˜
	static void println(int value) {
		System.out.println(value);
	}
	static void println(boolean value) {
		System.out.println(value);
	}
	static void println(double value) {
		System.out.println(value);
	}
	static void println(String value) {
		System.out.println(value);
	}
}

์˜ˆ์ œ) PrinterExample.java

public class PrinterExample {
	public static void main(String[] args) {
		Printer printer = new Printer();
		printer.println(10);
		printer.println(true);
		printer.println(5.7);
		printer.println("ํ™๊ธธ๋™");
	}
}

๊ฒฐ๊ณผ)

10
true
5.7
ํ™๊ธธ๋™

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