๐ this
- ๋ชจ๋ ์ธ์คํด์ค์ ๋ฉ์๋์ ์จ๊ฒจ์ง ์ฑ ์กด์ฌํ๋ ๋ ํผ๋ฐ์ค๋ก, ํ ๋น๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํจ๋ค.
- ์ฌ์ฉ ์ด์ : ์๊ธฐ ์์ ์ ํด๋์ค ๋ด๋ถ์ ์๋ ๋ณ์&๋ฉ์๋(this.), ์์ฑ์ํจ์(this())๋ฅผ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
- ์ญํ 1. ์ธ์คํด์ค ์์ ์ ์ฃผ์๋ฅผ ๋ฐํ.
- ์ญํ 2. this(): ์์ฑ์์์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํจ. ์์ฑ์ ์ค๋ฒ๋ก๋ฉ์์ ํ ์์ฑ์๊ฐ ๋ค๋ฅธ ์์ฑ์ ํธ์ถํ๋ ๊ฒฝ์ฐ.
- ์ญํ 3. ํจ์ ์คํ์ ์ ๋ฌ๋๋ ๊ฐ์ฒด์ ์ฃผ์๋ฅผ ์๋์ผ๋ก ๋ฐ๋๋ค.
this์ญํ 1. ์์ ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ฆฌํด. ๊ฐ์ ์์ฑ์๋ฅผ ๋ง๋ค์ด๋ ๋์ ์ธ์คํด์ค ์ฃผ์๋ ๋ค๋ฆ(date & date2)
๐public class MyDateTest {
public static void main(String[] args) {
MyDate date = new MyDate(); // ์ฌ๊ธฐ date
date.setYear(2019);
date.setMonth(77);
date.setDay(100);
date.showDate();
MyDate date2 = new MyDate(); // ์ฌ๊ธฐ date2์ ์ฃผ์๋ ๋ค๋ฆ.
date2.setYear(2002);
date2.showDate();
}
}
this์ญํ 2. ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ ๋, ๋ฐ๋์ ์ฒซ๋ฒ์งธ ์ค์์.
๐public class Person {
String name;
int age;
public Person() {
//age = 20 ; ๋ถ๊ฐ. why? ๋ค๋ฅธ statement ์ฌ ์ ์์. this๋ก ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ ๋, first statement์ฌ์ผ ํ๊ธฐ๋๋ฌธ์.
this("์ด๋ฆ์์" ,1); // ์ฌ๊ธฐ์ ๋ค๋ฅธ Person ์์ฑ์๋ฅผ ํธ์ถ.
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void showInfo() {
System.out.println(name+","+age);
}
public static void main(String[] args) {
Person personNoName=new Person();
personNoName.showInfo();
Person personLee = new Person("Lee",20);
personLee.showInfo();
}
}
๐ this์ญํ 3. ์์ ์ ์ฃผ์๋ฅผ ๋ฐํํ๋ this
public class Person {
String name;
int age;
public Person() {
this("์ด๋ฆ์์" ,1);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void showInfo() {
System.out.println(name+","+age);
}
public Person getSelf(){ // this๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ ๋ฐํํ์
์ ์๊ธฐ์์
return this;
}
public static void main(String[] args) {
Person personNoName=new Person();
personNoName.showInfo();
Person personLee = new Person("Lee",20);
personLee.showInfo();
System.out.println(personLee); //์ธ์คํด์ค ์ฃผ์๊ฐ ์ถ๋ ฅ : chapter5.Person@2ff4acd0
Person p = personLee.getSelf();
System.out.println(p); //์ธ์คํด์ค ์ฃผ์๊ฐ ์ถ๋ ฅ : chapter5.Person@2ff4acd0
// ์ฃผ์ ๊ฐ : ์ฐธ์กฐ๋ณ์์ this๊ฐ ๊ฐ๋ฆฌํค๋ ํ ๋ฉ๋ชจ๋ฆฌ๋ ๊ฐ๋ค.
}
}
ํ๋ก๊ทธ๋๋ฐ ํด๋ณด๊ธฐ
๐ป
public class House {
//this? ์๊ธฐ ์์ ์ ํด๋์ค ๋ด๋ถ์ ์๋ ๋ณ์, ๋ฉ์๋, ์์ฑ์ํจ์๋ฅผ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด ์ฌ์ฉ
public int price; //๊ฐ๊ฒฉ
public String dong; //์ํํธ (๋)
public int size; //ํ์
public String kind; //๋น๋ผ, ์ํํธ
public House() {
//this() : ์์ฑ์ ํจ์
//์์ฑ์ ํจ์ ๋ด์์ ์์ ์ ํด๋์ค ๋ด์ ์๋ ๋ค๋ฅธ ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฌ์ฉํ๊ฒ ๋ค.
this(100,32,"๋ํ๊ต","์คํผ์คํ
");
// price = 100;
// size =32;
// dong = "๋ํ๊ต";
// kind = "์ํํธ";
}
public House(int price) {
//์ง์ญ๋ณ์๊ฐ ์ฐ์ ์์๊ฐ ๋๊ธฐ ๋๋ฌธ์ ์๊ธฐ ์์ ํด๋์ค์ ๋ณ์๋ฅผ ๊ฐ๋ฆฌํค๋ this๋ฅผ ๋ช
์ํด์ฃผ์ด์ผํ๋ค.
//this.price=price; //but ์ด๊ฒ์ ํจ์จ์ด ๋จ์ด์ง
this(price,32,"๋ํ๊ต","๋น๋ผ"); //this()ํจ์๋ฅผ ์ฌ์ฉ
}
public House(int price, int size) {
this(price, size, "๋ํ๊ต", "์ฃผ์๋ณตํฉ");
}
public House(int price, int size, String dong) {
this(price, size, dong, "์ํํธ");
}
public House (int price, int size, String dong, String kind) {
//this. : ๋ณ์, ๋ฉ์๋
this.price = price;
this.size = size;
this.dong = dong;
this.kind = kind;
}
}
public class HouseConstructorTest {
public static void main(String[] args) {
House house1 = new House();
System.out.println(house1.price+":"+house1.size+":"+house1.dong+":"+house1.kind); //100:32:๋ํ๊ต:์คํผ์คํ
House house2 = new House(300);
System.out.println(house2.price+":"+house2.size+":"+house2.dong+":"+house2.kind); //300:32:๋ํ๊ต:๋น๋ผ
House house3 = new House(600,40);
System.out.println(house3.price+":"+house3.size+":"+house3.dong+":"+house3.kind); //600:40:๋ํ๊ต:์ฃผ์๋ณตํฉ
House house4 = new House(800,50,"์์ด๋");
System.out.println(house4.price+":"+house4.size+":"+house4.dong+":"+house4.kind); //800:50:์์ด๋:์ํํธ
House house5 = new House(10000,50,"์์ด๋","ํํธํ์ฐ์ค");
System.out.println(house5.price+":"+house5.size+":"+house5.dong+":"+house5.kind); //10000:50:์์ด๋:ํํธํ์ฐ์ค
}
}
'์ด๋ก > Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ ๊ทผ์ ์ด์์ ์ ๋ณด์๋ (0) | 2021.07.20 |
---|---|
์ฐธ์กฐ์๋ฃํ reference data type (0) | 2021.07.19 |
์์ฑ์ Constructor (0) | 2021.07.19 |
๊ฐ์ฒด ์งํฅ ์ธ์ด์ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ์ ํน์ง OOP Memory (0) | 2021.07.15 |
๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ Object-Oriented Programming(OOP)์ด๋ (0) | 2021.07.15 |