โJava
[Java ์ดํดํ๊ธฐ] 16. Java์ ์์ฑ์
Luvbit
2024. 5. 27. 11:31
๐ค Java์ ์์ฑ์๋?
์๋ฐ์ ์์ฑ์(constructor)๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋๋ ํน๋ณํ ๋ฉ์๋๋ก,
๊ฐ์ฒด์ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์์ฑ์๋ ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผํ ์ด๋ฆ์ ๊ฐ์ง๋ฉฐ ๋ฐํ ํ์ ์ด ์์ต๋๋ค
์์ฑ์๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ๋งค์ฐ ์ค์ํ ์ญํ ์ ํฉ๋๋ค.
๐ฉท์์ฑ์์ ํน์ง
1. ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผ
- ์์ฑ์๋ ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผํ ์ด๋ฆ์ ๊ฐ์ง๋๋ค.
- ๋ฐํ ํ์ ์ด ์์ผ๋ฉฐ, void๋ ์ฌ์ฉํ์ง ์์ต๋๋ค.
- Calculator๋ผ๋ ํด๋์ค์ ์์ฑ์๋ public Calculator() { } ํํ๋ก ์ ์ํฉ๋๋ค.
2. ๊ฐ์ฒด ์ด๊ธฐํ
- ์์ฑ์๋ ๊ฐ์ฒด์ ํ๋๋ฅผ ์ด๊ธฐํํ๊ฑฐ๋ ์ด๊ธฐ ์ค์ ์์ ์ ์ํํฉ๋๋ค.
- ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋๋ง๋ค ์คํ๋๋ฏ๋ก, ๊ฐ์ฒด๊ฐ ํ์ํ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋ ๋ฐ ์ ํฉํฉ๋๋ค.
- ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ฌ์ฉ์์ ํธ์์ ๋ฐ๋ผ ์ด๊ธฐํ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์กด์ฌํ ์ ์์ต๋๋ค.
3. ์ค๋ฒ๋ก๋ฉ ๊ฐ๋ฅ
- ์์ฑ์๋ ์ค๋ฒ๋ก๋ฉ๋ ์ ์์ต๋๋ค.
- ์ฆ, ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ด ๋ค๋ฅธ ์ฌ๋ฌ ์์ฑ์๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
- ์ด๋ฅผ ํตํด ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ์ ์์ต๋๋ค.
4 ๊ธฐ๋ณธ ์์ฑ์ ( =default constructor )
- ํด๋์ค์ ๋ช ์์ ์ผ๋ก ์์ฑ์๋ฅผ ์ ์ํ์ง ์์ผ๋ฉด,
- ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ ๊ณตํฉ๋๋ค.
- ๋ช ์์ ์ผ๋ก ์์ฑ์๋ฅผ ํ๋๋ผ๋ ์ ์ํ๋ฉด, ์ปดํ์ผ๋ฌ๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ ๊ณตํ์ง ์์ต๋๋ค.
5. this๋ฅผ ์ฌ์ฉํ ์์ฑ์ ํธ์ถ
- ์์ฑ์ ๋ด๋ถ์์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ ๋ this ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ์ด๋ฅผ ํตํด ์ค๋ณต ์ฝ๋๋ฅผ ์ค์ด๊ณ ์ด๊ธฐํ ๋ก์ง์ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ค๋ฒ๋ก๋ฉ๊ณผ this๋ ์ถํ ํฌ์คํ
์์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
๐งก์์ฑ์์ ์ฅ์
1. ๊ฐ์ฒด ์ด๊ธฐํ์ ์ผ๊ด์ฑ:
์์ฑ์๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํ์ํ ์ด๊ธฐํ ์์ ์ ์ผ๊ด๋๊ฒ ์ํํ ์ ์์ต๋๋ค.
์ด๋ฅผ ํตํด ๊ฐ์ฒด์ ์ํ๋ฅผ ์ผ๊ด์ฑ ์๊ฒ ์ ์งํ ์ ์์ต๋๋ค.
2. ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ:
์์ฑ์ ์ค๋ฒ๋ก๋ฉ๊ณผ this ํค์๋๋ฅผ ์ฌ์ฉํ ์์ฑ์ ํธ์ถ์ ํตํด ์ด๊ธฐํ ๋ก์ง์ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ ์ฝ๋ ์ค๋ณต์ ์ค์ด๊ณ ์ ์ง๋ณด์๋ฅผ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
3. ๊ฐ์ฒด ์์ฑ์ ํธ๋ฆฌํจ:
์์ฑ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด์ ๋์์ ํ์ํ ์ด๊ธฐํ ์์ ์ ์ํํ๋ฏ๋ก,
๊ฐ์ฒด ์์ฑ์ ๋ ๊ฐํธํ๊ณ ์ง๊ด์ ์ผ๋ก ๋ง๋ค ์ ์์ต๋๋ค.
ํน์ง๋ณ ์์ ๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์์ 1 ) ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผ
public class Example {
private int value;
// ์์ฑ์ - ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผํ๋ฉฐ ๋ฐํ ํ์
์ด ์์
public Example(int value) {
this.value = value;
}
public void displayValue() {
System.out.println("Value: " + value);
}
public static void main(String[] args) {
Example ex = new Example(10); // ์์ฑ์ ํธ์ถ
ex.displayValue(); // Value: 10 ์ถ๋ ฅ
}
}
์์ 2 ) ๊ฐ์ฒด ์ด๊ธฐํ
public class Person {
private String name;
private int age;
// ์์ฑ์ - ๊ฐ์ฒด ์ด๊ธฐํ
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Person person = new Person("pipi", 5); // ์์ฑ์ ํธ์ถ
person.displayInfo(); // Name: pipi, Age: 5 ์ถ๋ ฅ
}
}
์์ 3 ) ์ค๋ฒ๋ก๋ฉ ๊ฐ๋ฅ
public class Rectangle {
private int width;
private int height;
// ๊ธฐ๋ณธ ์์ฑ์
public Rectangle() {
this.width = 0;
this.height = 0;
}
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public void displayDimensions() {
System.out.println("Width: " + width + ", Height: " + height);
}
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(); // ๊ธฐ๋ณธ ์์ฑ์ ํธ์ถ
rect1.displayDimensions(); // Width: 0, Height: 0 ์ถ๋ ฅ
Rectangle rect2 = new Rectangle(10, 20); // ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์ ํธ์ถ
rect2.displayDimensions(); // Width: 10, Height: 20 ์ถ๋ ฅ
}
}
์์ 4 ) ๊ธฐ๋ณธ ์์ฑ์
public class Animal {
private String type;
// ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๋์ผ๋ก ์ ๊ณต๋จ
public Animal() {
this.type = "Unknown";
}
public void displayType() {
System.out.println("Type: " + type);
}
public static void main(String[] args) {
Animal animal = new Animal(); // ๊ธฐ๋ณธ ์์ฑ์ ํธ์ถ
animal.displayType(); // Type: Unknown ์ถ๋ ฅ
}
}
์์ 5 ) this๋ฅผ ์ฌ์ฉํ ์์ฑ์ ํธ์ถ
public class Car {
private String model;
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์
public Car(String model) {
this.model = model;
}
// ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ช
์์ ์ผ๋ก ์ ์
public Car() {
this.model = "Unknown";
}
public void displayModel() {
System.out.println("Model: " + model);
}
public static void main(String[] args) {
Car car1 = new Car("Tesla"); // ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์ ํธ์ถ
car1.displayModel(); // Model: Tesla ์ถ๋ ฅ
Car car2 = new Car(); // ๊ธฐ๋ณธ ์์ฑ์ ํธ์ถ
car2.displayModel(); // Model: Unknown ์ถ๋ ฅ
}
}
์์ฑ์๋ ๊ฐ์ฒด์ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋๋ฐ ๋งค์ฐ ์ค์ํ ์ญํ ์ ํฉ๋๋ค.
์ด๋ฅผ ํตํด ์ฝ๋์ ์ผ๊ด์ฑ๊ณผ ์ฌ์ฌ์ฉ์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
์ง๋ฌธ์ ์ธ์ ๋ ์ง ๋๊ธ์ ๋จ๊ฒจ์ฃผ์ธ์.
๐ฉต๐ฐ๐ฉต