Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์๋ฐ
- Codedeploy ์ค๋ฅ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- redis ํ ์คํธ์ฝ๋
- Kafka
- Entity
- docker compose
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- ํ์ดํผ๋ฐ์ด์
- docker ps -a
- docker
- AWS Certified Solutions Architect - Associate
- aws ์ฟ ํฐ
- prod docker-compose
- ๋ค์ค ์ปจํ ์ด๋
- JPA
- docker-compose kafka
- nGrinder
- ํ๋ก๊ทธ๋๋จธ์ค
- ์คํํ๋ ๋ฏธ์ค
- aws saa ํฉ๊ฒฉ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- s3 log ์ ์ฅ
- private subnet ec2 ๋ก์ปฌ ์ ์
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- redis ์กฐํ
Archives
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค ๋ณธ๋ฌธ
๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/๐๐๐ฏ๐
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค
๐คRyusun๐ค 2022. 10. 7. 16:17โญ๊ฐ์ฒด
- ๊ฐ์ฒด ์ ์
- object
- ๋ณผ ์ ์๊ณ ๋ง์ง ์ ์๋ ์ฌ๋ฌผ
- ์ถ์์ ์ผ๋ก ์๊ฐํ ์ ์๋ ๊ฒ
- ์์ ์ ์์ฑ์ ๊ฐ์ง๊ณ ์๊ณ ๋ค๋ฅธ ๊ฒ๊ณผ ์๋ณ ๊ฐ๋ฅํ ๊ฒ
- ๊ฐ์ฒด ๋ชจ๋ธ๋ง
- ํ์ค ์ธ๊ณ์ ๊ฐ์ฒด๋ฅผ ์ํํธ์จ์ด ๊ฐ์ฒด๋ก ์ค๊ณํ๋ ๊ฒ
- ํ์ค ์ธ๊ณ์ ๊ฐ์ฒด ์์ฑ๊ณผ ๋์ → ์ํํธ์จ์ด ๊ฐ์ฒด ํ๋์ ๋ฉ์๋ ์ ์
- ์ํํธ์จ์ด ์ธ๊ณ์ ๊ตฌํํ ๋์
- ํ๋ก๊ทธ๋จ ์๊ตฌ์กฐ๊ฑด์ ๋ฐ๋ผ ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒดํํ ๊ฒ์ธ์ง๋ ๋ฌ๋ผ์ง
- ๋ณดํต ์ํํธ์จ์ด ๊ธฐํ๋จ๊ณ์์ ๊ฒฐ์
- ๊ฐ์ฒด๋ค์ ๋ชจ๋ ํด๋์ค๋ฅผ ํตํด ์์ฑํ ์ ์์
- ๊ฐ์ฒด๋ ํ๋ (๋ฐ์ดํฐ)์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฅํ๊ฒ ํ๋ ๋ฉ์๋ (ํจ์)๋ฅผ ๊ฐ์ง
- Student ๊ฐ์ฒด๋ฅผ ์ค๊ณํด๋ณด์.
- ํ๋ - ์ด๋ฆ / ํ๋ฒ / ํ๊ธฐ / ์๋ ์์ผ / ์๊ฐ๊ณผ๋ชฉ ๋ฑ
- ๋ฉ์๋ - ์ด๋ฆ ๋ณ๊ฒฝํจ์ / ํ๋ฒ ๋ณ๊ฒฝํจ์ / ํ๊ธฐ ๋ณ๊ฒฝํจ์ / ์๊ฐ๊ณผ๋ชฉ ์ฒ ํํจ์
- ํ์๋ง๋ค ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ๋ ๋ค๋ฅผ ์ ์์
- ํด๋์ค - ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์ ์๋ ํ๋์ ๋ฉ์๋๋ ์ด๋ค ๊ฒ ์๋ค๋ผ๊ณ ์ ์ํด๋์ ์ค๊ณ๋
- ๊ฐ์ฒด - ์ค์ ์ค๊ณ๋๋ฅผ ํตํด ์ค์ ํ๋ํ๋ ๋์
- Student ๊ฐ์ฒด๋ฅผ ์ค๊ณํด๋ณด์.
- ๊ฐ์ฒด ์ถ๋ ฅ
- ๋ณดํต ๊ฐ์ฒด ์ถ๋ ฅํ๋ ์ด์ ๋ ๊ฐ์ฒด ๋ด์ฉ์ ๋ณด๊ธฐ ์ํด์์
- ๊ฐ์ฒด ์ด๋ฆ์๋ ๊ฐ์ฒด ๊ณ ์ ์ ID๊ฐ์ด ๋ค์ด์์
- ๊ฐ์ฒด ๋ด์ฉ์ ์ถ๋ ฅํ๊ธฐ ์ํด์๋ toString() ์ ์
@Override
public String toString() {
return "Student{" +
"ID='" + ID + '\'' +
", name='" + name + '\'' +
", currentSemester=" + currentSemester +
", takenCourses=" + Arrays.toString(takenCourses) +
'}';
- ๊ฐ์ฒด ๋น๊ต
- Object.hashcode()
- ๋ ๊ฐ์ฒด๊ฐ ๋ค๋ฅด๋ฉด ๊ฐ์ฒด ๊ณ ์ ID๊ฐ์ธ hascode๋ ๋ค๋ฆ
- ๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ ๋ ๋จผ์ hashcode๋ก ๋น๊ตํ๋ฉด ๋น ๋ฅด๊ฒ ์ฐ์ฐ ๊ฐ๋ฅ
- hashcode๊ฐ ๋ค๋ฅด๋ฉด ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ง ์์
- hashcode๊ฐ ๊ฐ์ผ๋ฉด ๋ ๊ฐ์ฒด๊ฐ ๊ฐ๊ฑฐ๋ ๋ค๋ฅผ ์๋ ์์
- ๊ฐ์ฒด ๊ณ ์ ID๊ฐ์ด ๊ฐ๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ ์๋ ์์
- ๊ฐ์ฒด ๊ณ ์ ID๊ฐ์ด ๊ฐ์๋ ๋ด์ฉ๋ฌผ์ด ๋ค๋ฅผ ์ ์์
- ๋ณดํต hashing ์๊ณ ๋ฆฌ์ฆ์ด ์๋ชป ๋์๊ฑฐ๋ ์ถฉ๋์ด ์ผ์ด๋๋ ๊ฒฝ์ฐ์
- Object.equals()
- ๋ ๊ฐ์ฒด๊ฐ equals()์ ์ํด ๋์ผํ๋ค๋ฉด ๋ ๊ฐ์ฒด์ hashCode() ๊ฐ๋ ์ผ์นํด์ผ ํจ
- ๋ด์ฉ๋ฌผ์ด ๋ค ๊ฐ๋ค๋ ์๋ฆฌ๋ ๊ฐ์ ๊ฐ์ฒด์์ ์๋ฏธ
- ๋ ๊ฐ์ฒด๊ฐ equals()์ ์ํด ๋์ผํ์ง ์๋ค๋ฉด ๋ ๊ฐ์ฒด์ hashCode() ๊ฐ์ ์ผ์นํ์ง ์์๋ ๋จ
- ๋ค๋ฅธ ๊ฐ์ ๋ฆฌํดํ๋ฉด hash table ์ฑ๋ฅ ํฅ์
- equals()์ฌ์ ์ํ๋ฉด hashCode()๋ ์ฌ์ ์ํด์ผ ํจ
- ๊ฐ์ ๊ฐ์ฒด์ ๋ํด์ ๊ฐ์ ๊ฐ์ ๋ฆฌํดํ๋๋ก ๊ตฌํํด์ผ ํฉ
- ๋ ๊ฐ์ฒด๊ฐ equals()์ ์ํด ๋์ผํ๋ค๋ฉด ๋ ๊ฐ์ฒด์ hashCode() ๊ฐ๋ ์ผ์นํด์ผ ํจ
- Object.hashcode()
- ๊ฐ์ฒด ๋ณต์ฌ
- Object.clone()
- ๊ฐ์ฒด ๋ณต์ฌ๋ Cloneable์ด๋ผ๋ ์ธํฐํ์ด์ค์ clone ํจ์ ์ฌ์ ์ ๊ฐ๋ฅ
- static newInstance() ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ํตํด ์์ฑํ ์๋ ์์
- ๊ฐ์ฒด ๋ณต์ฌ๋ ๊ฐ์ฒด ๋ด๋ถ์ ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด๊ฐ ์๋ ๊ฒฝ์ฐ์๋ deep copy ์ํ
- Object.clone()
@Override
public ObjectCopyExample clone() throws CloneNotSupportedException {
ObjectCopyExample objectCopyExample = (ObjectCopyExample) super.clone(); // shallow copy
objectCopyExample.personArray = new Person[personArray.length];
for (int i = 0; i < objectCopyExample.personArray.length; i++) {
// objectCopyExample.personArray[i] = new Person();
// objectCopyExample.personArray[i].setName(personArray[i].getName());
// objectCopyExample.personArray[i].setGender(personArray[i].getGender());
// objectCopyExample.personArray[i].setBirthdate(personArray[i].getBirthdate());
// objectCopyExample.personArray[i].setRegistrationNumber(personArray[i].getRegistrationNumber());
objectCopyExample.personArray[i] = personArray[i];
// ๋ฐฐ์ด ์ด๋ฆ์ ์ฃผ์ ์ ์ ์ด ๋์ด์์ผ๋ฏ๋ก ์ฃผ์ ๋ณต์ฌ๊ฐ ๋จ (์๋ชป๋ ๋ณต์ฌ)
}
return objectCopyExample;
}
โญ์์ฑ์
์ ์
- new ์ฐ์ฐ์๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ ๋ฉ์๋
- ๊ฐ์ฒด ์์ฑ๊ณผ ๋์์ ์ํ๋ ๊ฐ์ผ๋ก ์ด๊ธฐํํ ์ ์๋ ๋ฉ์๋
- ์์
- ํ์ฌ ๊ฐ๋ฐ๋ถ์์ 5๋ช TO๊ฐ ์๋๋ฐ 2๋ช ์ ํ์ ์ด๊ณ 3๋ช ์ ์์ง ์ฑ์ฉ์ค์ด๋ผ๊ณ ๊ฐ์
1 ํ๊ธธ๋ ๊ฐ๋ฐํ ๋๋ฆฌ 20220901 2 ํ๊ธธ์ ๊ฐ๋ฐํ ์ฃผ์ 20210501 3 ๊น์๋ฐ ๊ฐ๋ฐํ ๊ณผ์ฅ 20190801 4 ์ธํ ๋ฆฌ ๊ฐ๋ฐํ ํ์ฅ 20200303 5 ๋ฐ์ ์ด ๊ฐ๋ฐํ ์ฌ์ 20210303 6 ์ต์ ์ ๊ฐ๋ฐํ ์ฌ์ ? 7 ์ด์ ์ ๊ฐ๋ฐํ ๊ณผ์ฅ ? 8 ? ๊ฐ๋ฐํ ? ? 9 ? ๊ฐ๋ฐํ ? ? 10 ? ๊ฐ๋ฐํ ? ? - ์ฌ์๋ฒํธ๋ ์๋์์ฑ
- ๋ถ์๋ ๊ฐ๋ฐํ์ด ๊ธฐ๋ณธ๊ฐ
- 6, 7๋ฒ์ public Employee(String name, String position)์ผ๋ก ์์ฑ
- 8 ~ 10๋ฒ์ public Employee()์ผ๋ก ์์ฑ
ํน์ง
- ์์ฑ์ ์ด๋ฆ์ ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผํ๋ฉฐ ๋์ผํ์ง ์์ผ๋ฉด ์คํํ ๋ ์ค๋ฅ ๋ฐ์ํ๋ค.
- ๋๋ถ๋ถ์ ํด๋์ค์์ ๋ณดํต ์์ฑ์๋ ์ค๋ฒ๋ก๋ฉ์ด ๋ง์ด ๋์ด์์
- ๊ฐ์ฒด ์์ฑ์ ๋ค์ํ๊ฒ ์์ฑํ ์ ์๋๋ก ํ๊ธฐ ์ํจ
- ์์ฑ์ ์ ์๋ฅผ ํ์ง ์์ ํด๋์ค๋ ์๋ฐ ์ปดํ์ผ๋ฌ๊ฐ ๊ธฐ๋ณธ ์์ฑ์ ์๋ ์์ฑํจ
- ์์ฑ์ ์ ์๋ฅผ ํ๋๋ผ๋ ํ ํด๋์ค๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์๋์์ฑ๋์ง ์๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ ์์ฑ์๋ ์ ์ํ์ํจ
class A {}
class B {}
public class ConstructorNotGoodCode {
private A a;
private B b;
// ์์ข์
public ConstructorNotGoodCode() {
a = new A();
b = new B();
}
// ์ด ์ฝ๋๊ฐ ๋ ์ข์
public ConstructorNotGoodCode(A a, B b) {
this.a = a;
this.b = b;
โญํด๋์ค
- ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํ ์ค๊ณ๋
- ์ ํ์ํ ๊น ?
- ํ์์ด n๋ช ์๋ค๊ณ ๊ฐ์
- ๋ชจ๋ ํ์์ ํ๋ฒ / ์ด๋ฆ / ํ๋ / ์๋ ์์ผ์ ์ ์ฅํ๊ณ ์๋ค๊ณ ๊ฐ์ ํ์.
- ํ์ n๋ช ๋ชจ๋ ๋์ผํ ํ๋๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ํ์ ๋ง๋ค์ด๋๊ณ ๊ทธ ํ์ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํจ
- ์ฌ๊ธฐ์ ํ์ด ํด๋์ค ๐ฅ
- ํด๋์ค ํ์ ๋ง๋ค์ด๋๊ณ ๊ฐ์ฒด๋ฅผ ์ฐ์ด ์์ฐํจ
- ํด๋์ค ํ์ด ๋์ผํ๋ฉด ๊ฐ์ฒด๋ค์ ๋์ผํ ํ๋๋ฅผ ๊ฐ์ง
- ์ ์ธ๋ฒ
- ํ๋ ์ด์ ๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ์ผ ํจ
- ์ฒซ๋ฒ์งธ ๊ธ์๋ ์ซ์๊ฐ ์ฌ ์ ์์
- ‘$’, ‘_’ ์ธ์ ํน์๋ฌธ์ ์ฌ์ฉ ๋ถ๊ฐ
- ํค์๋ ์ฌ์ฉ๋ถ๊ฐ
- ํ๋์ ํด๋์ค๋ ์ฌ๋ฌ๊ฐ์ ํ๋์ ๋ฉ์๋๊ฐ ์์ ์ ์์
public class Member {
// ํ๋ (๋ฐ์ดํฐ, ๋ฉค๋ฒ๋ณ์)
String name;
int age;
String gender;
// ๋ฉ์๋ (๋ฐ์ดํฐ๋ฅผ ๋์ํ๊ฒ ํ๋ ๊ฒ, ํจ์)
public void increaseAge() { // ๋ด๋
์ด ๋์ด ๋์ด๊ฐ ํ ์ด ๋ ๋จน์ผ๋ฉด ...? ๐ฅฒ
age += 1;
}
}
โญ์ธ์คํด์ค
- ํด๋์ค๋ฅผ ๋ฐํ์ผ๋ก ์ค์ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น๋์ด ์ฌ์ฉ๋๋ ์ค์ฒด
- ๊ฐ์ฒด๋ฅผ ์ํํธ์จ์ด์ ์ค์ฒดํํ๋ฉด ์ธ์คํด์ค๋ผ๊ณ ๋ถ๋ฆ
- ์ผ๋ฐ์ ์ธ ๋ป
- ์ถ์์ ์ธ ๊ฐ๋ ๊ณผ ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด ์ฌ์ด์ ๊ด๊ณ์ ์ด์ - ‘~์ ์ธ์คํด์ค’
- ๊ฐ์ฒด๋ ํด๋์ค์ ์ธ์คํด์ค
- ์ด๋ค ์๋ณธ (์ถ์์ ์ธ ๊ฐ๋
)์ผ๋ก๋ถํฐ ‘์์ฑ๋ ๋ณต์ ๋ณธ'
- ์ค์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋นํ๋ฉด์ ํ๋ํ๊ณ ์๋ ์ค์ฒด๋ฌผ
public class Main {
public static void main(String[] args) {
Student student1; // ๊ฐ์ฒด ๋ณ์
Student student2; // ๊ฐ์ฒด ๋ณ์
// ์ธ์คํด์คํ
student1 = new Student("0001", "Jonh", 30); // student1๋ Student ํด๋์ค์ ์ธ์คํด์คํ (๊ฐ์ฒด๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น)
student2 = new Student("0002", "James", 50); // student2๋ Student ํด๋์ค์ ์ธ์คํด์คํ (๊ฐ์ฒด๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น)
}
}
โญ์ ๋ฆฌ
- ํด๋์ค vs ๊ฐ์ฒด
- ํด๋์ค - ์ค๊ณ๋
- ๊ฐ์ฒด - ์ค๊ณ๋๋ก ๊ตฌํํ ๋ชจ๋ ๋์
- ๊ฐ์ฒด vs ์ธ์คํด์ค
- ํด๋์ค ํ์
์ผ๋ก ์ ์ธ๋์์ ๋ ๊ฐ์ฒด๋ผ๊ณ ๋ถ๋ฆ Student student
- ๊ฐ์ฒด๋ณ์๋ผ๊ณ ๋ ๋ถ๋ฆ
- ๊ฐ์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น๋์ด ์ค์ ์ฌ์ฉ๋ ๋ ์ธ์คํด์ค๋ผ๊ณ ๋ถ๋ฆ student = new Student()
- ๊ฐ์ฒด๊ฐ ์์ฑ๋์๊ณ ์ฌ์ฉํ ์ ์๋ค๋ ์๋ฏธ๋ก ์ธ์คํด์ค๋ผ๊ณ ํจ
- ํด๋์ค ํ์
์ผ๋ก ์ ์ธ๋์์ ๋ ๊ฐ์ฒด๋ผ๊ณ ๋ถ๋ฆ Student student
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐๐ฏ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Generic Method ์ ๋ค๋ฆญ ๋ฉ์๋ (0) | 2022.10.12 |
---|---|
[Java] ์บก์ํ (0) | 2022.10.07 |
[Java] ๋ฐฐ์ด (6) | 2022.09.30 |
[Java] ๋ฌธ์์ด (0) | 2022.09.30 |
[Java] ์กฐ๊ฑด๋ฌธ (0) | 2022.09.30 |