์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- java
- serverless
- Pandas
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- JavaScript
- ํ์ด์ฌ
- Streamlit๊ธฐ์ด
- Android
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- streamlit
- ์๋ฐ๊ธฐ์ด
- ์คํธ๋ฆผ๋ฆฟ
- ์๋๋ก์ด๋ ์คํ๋์ค
- ํ๋ค์ค
- Streamlit๊ธฐ๋ณธ
- db
- ์๋ฐ๊ณต๋ถ
- RESTful API
- ํ์ด์ฌ๊ณต๋ถ
- Matplotlib ๊ธฐ์ด
- ์๋ฐ์ด๋ณด
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ์๋ฐ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์น๋์๋ณด๋ ๊ธฐ์ด
- ์น๋์๋ณด๋ ์ ์
- ํ๋ค์ค๊ณต๋ถ
- MySQL
- Today
- Total
ruriruriya
[Java]์๋ฐ์ ์์ฑ์(Constructor)์ Getter/Setter ๋ณธ๋ฌธ
์๋ฐ์์๋ ์์ฑ์๋ก ๊ณต๊ฐ์ ๋จผ์ ํ์ฑํ๋ค.
๊ทธ๋ฆฌ๊ณ ํ์ฑ๋ ๊ณต๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ณ
๋ณ์๋ก ๊ทธ ๊ณต๊ฐ์ ๊ฐ๋ฆฌํจ๋ค.
1. ํด๋์ค ์์ฑ
ํด๋์ค ํ๋๋ฅผ ์์ฑํด์ ๋ณ์๋ฅผ ์ ์ธํ๊ณ ํจ์๋ฅผ ๋ง๋ ๋ค.
๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐํ ํจ์์, print ํจ์ 2๊ฐ๋ฅผ ๋ง๋ ๋ค.
public class Member {
String name;
String tel;
String address;
Member(){
// ๋์๊ฐ๊ฒ ํ๊ธฐ ์ํด์ ๋ง๋ ๊ธฐ๋ณธ ์์ฑ์.
}
Member(String name, String tel, String address){
this.name = name;
this.tel = tel;
this.address = address;
}
void print(){
System.out.println(name + " " + tel + " " + address);
}
}
2. ๋ฉ์ธ ํด๋์ค ์์ฑ
์์ฑ์๋ก ๊ณต๊ฐ์ ์์ฑํ๊ณ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ ์ถ๋ ฅํ๋ ๋ฐฉ์๊ณผ
๋งค๊ฐ๋ณ์๋ฅผ ๋ฃ์ด ์ถ๋ ฅํ๋ ๋ฐฉ์, 2๊ฐ์ง๋ก ์ถ๋ ฅ์ ํด๋ณธ๋ค.
public class MemberTest {
public static void main(String[] args) {
Member m1 = new Member(); // 1. ๊ณต๊ฐ์์ฑ
m1.name = "ํ๊ธธ๋";
m1.tel = "010-1111-2222";
m1.address = "์ธ์ฒ์ ์๊ตฌ ๊ฐ์ ๋";
Member m2 = new Member("๋๊ตฌ","010-1234-5467","์ด๋๊ฐ ์ด์ด์");
m1.print();
m2.print();
}
}
3. getter/setter
๋จผ์ Member ํด๋์ค์์ get/set ํจ์๋ฅผ ์ธํ ํ๋ค.
getter๋ return์ด ์๊ณ
setter๋ return์ด ์์ด์ void๋ฅผ ์
๋ ฅํด์ผ ํ๋ค.
public class Member {
String name;
String tel;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
๋ฉ์ธ ํด๋์ค์์๋
์์ฑ์๋ฅผ ๋ง๋ค์ด ๋ณ์์ ๋ฃ์ด์ฃผ๊ณ
set์ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ๋ฃ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์๋ get์ผ๋ก ๊ทธ ๊ฐ์ ํ๋ฆฐํธ ํด๋ณธ๋ค.
public class MemberTest {
public static void main(String[] args) {
Member m4 = new Member();
//setter
m4.setName("์ฒ ์");
m4.setTel("010-1111-1111");
m4.setAddress("์ธ์ฒ ์๊ตฌ");
//getter
m4.getName();
System.out.println(m4.getName());
}
}
๊ทธ๋ผ set์ผ๋ก ๋ง๋ค์๋ ๊ฐ์ ์ถ๋ ฅํ๋ค.
'๐ชJava' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์๋ฐ ๋ค๋ฅธ ๋ฐ์ดํฐํ์ ์ ๋ฌธ์์ด๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ๋ค ์ ๋ฆฌ (0) | 2024.02.07 |
---|---|
[Java] ์๋ฐ์ ๋ค์ํ ์ฐ์ฐ์๋ค (0) | 2024.02.05 |
[Java] ์๋ฐ ํด๋์ค(๋ฉ์ธ ํด๋์ค/ํด๋์ค) (0) | 2023.12.20 |
[JAVA] ์๋ฐ ๋ฆฌ์คํธ(List)์ ArrayList (0) | 2023.11.13 |
[JAVA] ์๋ฐ ์คํธ๋ง๋ฒํผ(StringBuffer) (1) | 2023.11.13 |