[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
- ์ธ์คํด์ค ๋ณ์ ์ด๊ธฐํ ์์ : ๊ธฐ๋ณธ๊ฐ -> ๋ช ์์ ์ด๊ธฐํ -> ์ธ์คํด์ค ์ด๊ธฐํ ๋ธ๋ญ -> ์์ฑ์
- ํด๋์ค ๋ณ์ ์ด๊ธฐํ ์์ : ๊ธฐ๋ณธ๊ฐ -> ๋ช ์์ ์ด๊ธฐํ -> ํด๋์ค ์ด๊ธฐํ ๋ธ๋ญ
์ต๊ทผ๋๊ธ