[Day16] Java 16 [8/30]

 

1.  ๊ฐ€๋ณ€์ธ์ž(varagrgs)์™€ ์˜ค๋ฒ„๋กœ๋”ฉ

- variable(๋ณ€์ˆ˜) + arguments(์ธ์ž, ํŒŒ๋ผ๋ฏธํ„ฐ, ๋งค๊ฐœ๋ณ€์ˆ˜, ์ธ์ˆ˜)

- ๊ฐ€๋ณ€์ธ์ž ‘…’

public static int add (int … m) {  // integer ๋ฐฐ์—ด m๊ณผ ๋™์ผ

-> ์ธ์ž๊ฐ€๋ช‡๊ฐœ๋“  ๋‹ค ์ฒ˜๋ฆฌ๊ฐ€๋Šฅ, ์˜ค๋ฒ„๋กœ๋”ฉ ํ•  ํ•„์š” ์—†์ด ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ช‡๊ฐœ๊ฐ€ ๋ ์ง€ ๋ชจ๋ฅธ๋‹ค๋ฉด ๊ฐ€๋ณ€์ธ์ž ์‚ฌ์šฉํ•˜๋ฉด๋จ

String concatenate(String s1, String s2) {}
String concatenate(String s1, String s2, String s3) {}
String concatenate(String s1, String s2, String s3, String s4) {}

String concatenate(String... str) {}

 

2.  getter setter

 [  private  ๋กœ ์„ ์–ธ๋œ ํ•„๋“œ๋ฅผ  ์ ‘๊ทผํ•˜๋Š”  ๋ฐฉ๋ฒ• ? ]      (private int x, y)

-> getter, setter ๋ฉ”์„œ๋“œ 

1.   

- getter ๋งŒ ์„ ์–ธ -  private x   ์ฝ๊ธฐ ๊ฐ€๋Šฅ,   ์“ฐ๊ธฐ X

- setter ๋งŒ ์„ ์–ธ -                   ์ฝ๊ธฐ X, ์“ฐ๊ธฐ O

public String getName() {
		return name;
	}

public void setName(String name) {
		this.name = name;
	}

 

2.   ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ›„  ์œ ํšจํ•œ ๊ฐ’๋งŒ์„ ํ• ๋‹นํ•˜๊ณ ์ž ํ•  ๋•Œ ->  setter ํ†ตํ•ด์„œ ์ฒ˜๋ฆฌ

public int getY() {
	return y;
}

// ***** this ํ‚ค์›Œ๋“œ ****
public void setY(int b) {
	if( b >= 0  && b <= 100)		y = b;
	else  System.out.println("y ์ขŒํ‘œ๊ฐ’์˜ ๋ฒ”์œ„๊ฐ€ ๋ฒ—์–ด๋‚ฌ๋‹ค.");
}

 

3.  ์ƒ์„ฑ์ž (constructor)

1. ๋ฉ”์„œ๋“œ

2. ๋ฐ˜๋“œ์‹œ ์ƒ์„ฑ์ž์ด๋ฆ„ == ํด๋ž˜์Šค ์ด๋ฆ„

3. ๋ฆฌํ„ด์ž๋ฃŒํ˜•์„ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.  void X

4. ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์‹œ๊ธฐ ? ์ธ์œ„์ ์œผ๋กœ ํ˜ธ์ถœํ• ์ˆ˜ ์—†์–ด์š”. -> ์–ธ์ œ ํ˜ธ์ถœ ? ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.  

5. ์ƒ์„ฑ์ž ์—ญํ• ? ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• 

6. ์ ‘๊ทผ์ง€์ •์ž 4๊ฐ€์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.  + ์ƒ์„ฑ์ž                 

7.  " ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ " ๋ผ๊ณ ๋„ ํ•œ๋‹ค. 

8. ์˜ค๋ฒ„๋กœ๋”ฉ ๊ฐ€๋Šฅ

9. ๋งค๊ฐœ๋ณ€์ˆ˜ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ "๋””ํดํŠธ์ƒ์„ฑ์ž"๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

10. ์ƒ์„ฑ์ž๋Š” ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค.

11. ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ํ•  ๋•Œ ์ž๋™์œผ๋กœ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

public MyPoint( ) {
	// ํ•„๋“œ ์ดˆ๊ธฐํ™” ์—ญํ• 
	x = y = 100;
} 

public MyPoint(int i) {
	// ํ•„๋“œ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ์šฉ๋„๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.
	 x = y = i;
}
	
// ์ƒ์„ฑ์ž ์„ ์–ธ
public MyPoint(int a, int b) {
	// ํ•„๋“œ ์ดˆ๊ธฐํ™” ์—ญํ• 
	x = a;
	y = b;
} 

public MyPoint(MyPoint p1) {
	x = p1.x;
	y = p1.y;
}

 

4.  this

1)  ์ •์˜ ?  ์ฐธ์กฐ๋ณ€์ˆ˜ == ์ €์žฅ๊ณต๊ฐ„ -> ํด๋ž˜์Šค ์ž๊ธฐ ์ž์‹ ์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ–๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜. 

2) ์‚ฌ์šฉ์ž  ์ €์žฅ๊ณต๊ฐ„ ์ƒ์„ฑ = ๊ฐ’ ํ• ๋‹น X                

3) this 3๊ฐ€์ง€ ์šฉ๋„

-    ์ธ์Šคํ„ด์Šค(ํด๋ž˜์Šค ์ž๊ธฐ์ž์‹ )์˜ ๋ฉค๋ฒ„๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ๋•Œ์˜ this

-     ์ƒ์„ฑ์ž์—์„œ ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ์˜ this ์šฉ๋„

-     ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉ๋  ๋•Œ์˜ this ์šฉ๋„ ๋ฆฌํ„ด๊ฐ’, ๋งค๊ฐœ๋ณ€์ˆ˜

 

class Car{
	
	// ํ•„๋“œ
	private String color; // ์ƒ‰์ƒ
	private String gearType; // ๋ณ€์†๊ธฐ ์ข…๋ฅ˜ - ์ˆ˜๋™, ์ž๋™
	private int door;      // ์ฐจ๋ฌธ
	
	// getter, setter ์„ ์–ธ    Alt + Shift + S
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;   // this์˜ ์ฒซ ๋ฒˆ์งธ ์šฉ๋„
	}
	public String getGearType() {
		return gearType;  
	}
	public void setGearType(String gearType) {
		this.gearType = gearType;  // this์˜ ์ฒซ ๋ฒˆ์งธ ์šฉ๋„
	} 
	
	// ๋””ํดํŠธ ์ƒ์„ฑ์ž
	public Car() {		
	   this("ํฐ์ƒ‰","์ž๋™", 4);
	   System.out.println("> Car ๋””ํดํŠธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ๋จ.");
	}
    
	// ์ƒ์„ฑ์ž + 2๊ฐœ
	public Car(String color, String gearType) {
    	this(color, gearType, 4); // ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ์šฉ๋„
        // The assignment to variable color has no effect
        // Car ํด๋ž˜์Šค์˜ ํ•„๋“œ color
        // this.color= color;  // this์˜ ์ฒซ ๋ฒˆ์งธ ์šฉ๋„
        // this.gearType = gearType;
        System.out.println("> Car + 2 ์ƒ์„ฑ์ž ํ˜ธ์ถœ๋จ.");
        
        // this.ํ•„๋“œ, ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
        // ํด๋ž˜์Šค ์ž๊ธฐ ์ž์‹ ์˜ ๋ฉค๋ฒ„๋“ค์ด  this.   ๋ณด์ธ๋‹ค. 
        // this.toString()      
	} 
	
	public Car(String color, String gearType, int door) {	

		this.color = color;
		this.gearType = gearType;
		this.door = door;
		
		 System.out.println("> Car + 3 ์ƒ์„ฑ์ž ํ˜ธ์ถœ๋จ.");
	}
	
	
	// ๋ฉ”์„œ๋“œ
	// mycar.dispCar();
	public void dispCar() {
		System.out.printf("> color:%s, gearType:%s \n", this.color, this.gearType);
	}
	
	// ์ฐจ๋Ÿ‰์˜ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์„œ๋“œ
	// [์ฐธ์กฐํ˜•์ด ๋ฐ˜ํ™˜ํƒ€์ž…]
	// mycar.chageColor("๋…ธ๋ž‘");
	// changeColor()  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด : mycar
	public Car changeColor( String color) {
		this.color = color;
		return this;  // mycar  ์ฐธ์กฐ๋ณ€์ˆ˜  0x100 ์ฃผ์†Œ, this์˜ ์„ธ ๋ฒˆ์งธ ์šฉ๋„
	}
	
} // class

 

- alt shift s = ์ž๋™์œผ๋กœ ์ƒ์„ฑ์ž 3๊ฐœ์งœ๋ฆฌ ๋งŒ๋“ค์–ด์ง

 - return this = ์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ž๊ธฐ์ž์‹  ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ฐ’์—๋‹ค๊ฐ€ ๋˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฉ”์„œ๋“œ ์ฒด์ธ ๊ฐ€๋Šฅ

 ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹? : https://advenoh.tistory.com/4

 

5.  ์ œ์–ด์ž( modifier )  - static

- ํด๋ž˜์Šค, ๋ณ€์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์„ ์–ธ๋ถ€์— ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ๋ถ€๊ฐ€์ ์ธ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค. 

- ์ ‘์ง€( 4 ) + ๊ธฐ์ œ : [static], final, abstract, native, transient, synchronized, volatile, strictfp

 

static ๋ฉ”์„œ๋“œ

-> rate๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘๊ณผ ๋™์‹œ์— ์˜ฌ๋ผ๊ฐ€์žˆ์ง€๋งŒ ํด๋ž˜์Šค๋ช….ํด๋ž˜์Šค์ด๋ฆ„ ํ•ด๋„ ์•ˆ๋‚˜์˜ด

                ------> ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ์ง€ ์•Š์€๊ฒŒ ์•„๋‹ˆ๋ผ ์ ‘๊ทผ ์ง€์ •์ž๋•Œ๋ฌธ์— ์ ‘๊ทผ์„ ๋ชปํ•˜๋Š”๊ฒƒ

-> ์ƒ์„ฑ ์•ˆํ•ด๋„ ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘๋งŒํ•ด๋„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ

-> ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘๊ณผ ๋™์‹œ์— ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€๋Š” static ๋ฉ”์„œ๋“œ์— this ์ ์„ ์ˆ˜ ์—†์Œ 

                 ------> this๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•ด์•ผ ์ฃผ์†Œ๊ฐ’ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ

-> static ํด๋ž˜์Šค๋ณ€์ˆ˜ ์„ ์–ธํ•˜๋Š” ์ด์œ 

                 -------> ๋ชจ๋“  ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)์—์„œ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ• ๋•Œ static ๋ณ€์ˆ˜ ์„ ์–ธ

 -> static ๋ฉ”์„œ๋“œ = ์ •์  ๋ฉ”์„œ๋“œ ์„ ์–ธํ•˜๋Š” ์ด์œ 

                --------> ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ•„์š”์—†์ด ํด๋ž˜์Šค๋ช…์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ(๋ฉ”์„œ๋“œ) ์žˆ๋‹ค๋ฉด static ๋ฉ”์„œ๋“œ๋กœ ์„ ์–ธ

 

6.  ํ•„๋“œ ์ดˆ๊ธฐํ™”

[ ํ•„๋“œ ์ดˆ๊ธฐํ™” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• ]

1)  ๋ช…์‹œ์  ์ดˆ๊ธฐํ™”( explicit initialization ) :

- ํ•„๋“œ๋ฅผ ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์„ "๋ช…์‹œ์  ์ดˆ๊ธฐํ™”"๋ผ๊ณ  ํ•œ๋‹ค. 

- ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ ํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๋จ

String name = "๋ฏธ์ƒ";
int age = 20;
static double rate = 0.04;
	
int count;           // ๋ช…์‹œ์  ์ดˆ๊ธฐํ™” x, ๊ธฐ๋ณธ๊ฐ’
int serialNo;       // ๋ช…์‹œ์  ์ดˆ๊ธฐํ™” x, ๊ธฐ๋ณธ๊ฐ’

2) ์ƒ์„ฑ์ž( constructor )

// constructor
Person(){
	//count++;
	//serialNo = count;
	System.out.println("> Person ๋””ํดํŠธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ.");
}	

Person(String name, int age, double rate){
	//count++;
	//serialNo = count;

	this.name = name;
	this.age = age;
	Person.rate = rate;
	System.out.println("> Person 3 ์ƒ์„ฑ์ž ํ˜ธ์ถœ.");
}

3) ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ( initialization block ) ใ…ฃ  ๋ณต์žกํ•œ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉ.  ๋ชจ๋“  ์ƒ์„ฑ์ž์—์„œ ๊ณตํ†ต์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์žฅ์„ ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ.

          - ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ : ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

          - ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ    : ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”.

{
	// ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ
	// ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด)๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๋งˆ๋‹ค ์‹คํ–‰
	count++;
	serialNo = count;
	System.out.println("> ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ.");
}	
static {
	// ํด๋ž˜์Šค๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ
	// ์ฒซ ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋   ๋•Œ 1๋ฒˆ ์‹คํ–‰
	rate = 0.08;	 
	System.out.println("> ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ.");
}

 

p.304

- ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ์ˆœ์„œ  : ๊ธฐ๋ณธ๊ฐ’ -> ๋ช…์‹œ์  ์ดˆ๊ธฐํ™” -> ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ -> ์ƒ์„ฑ์ž

- ํด๋ž˜์Šค ๋ณ€์ˆ˜    ์ดˆ๊ธฐํ™” ์ˆœ์„œ : ๊ธฐ๋ณธ๊ฐ’ -> ๋ช…์‹œ์ ์ดˆ๊ธฐํ™” -> ํด๋ž˜์Šค ์ดˆ๊ธฐํ™” ๋ธ”๋Ÿญ 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ