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
- Entity
- redis ์กฐํ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- ํ์ดํผ๋ฐ์ด์
- redis ํ ์คํธ์ฝ๋
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- ์คํํ๋ ๋ฏธ์ค
- AWS Certified Solutions Architect - Associate
- Kafka
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- docker compose
- docker-compose kafka
- ํ๋ก๊ทธ๋๋จธ์ค
- ์๋ฐ
- Codedeploy ์ค๋ฅ
- JPA
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- ๋ค์ค ์ปจํ ์ด๋
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- docker ps -a
- aws saa ํฉ๊ฒฉ
- aws ์ฟ ํฐ
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- docker
- prod docker-compose
- private subnet ec2 ๋ก์ปฌ ์ ์
- nGrinder
- s3 log ์ ์ฅ
- s3 ์ด๋ฏธ์ง ์ ์ฅ
Archives
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Java] ์บก์ํ ๋ณธ๋ฌธ
๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/๐๐๐ฏ๐
[Java] ์บก์ํ
๐คRyusun๐ค 2022. 10. 7. 17:12โญ ์บก์ํ
์ ์
- ๊ด๋ จ์ด ์๋ ํ๋์ ๋ฉ์๋๋ฅผ ํ๋๋ก ๋ฌถ๊ณ ์ธ๋ถ์์ ์ฝ๊ฒ ์ ๊ทผํ์ง ๋ชปํ๋๋ก ๊ตฌํํ๋ ๋ฐฉ๋ฒ
- ๊ฐ์ฒด์ ํ๋์ ์ง์ ์ ์ธ ์ ๊ทผ์ ๋ง๊ณ ์ธ๋ถ์์ ๋ด๋ถ์ ์ ๋ณด๋ฅผ ๋ณ๊ฒฝํ์ง ๋ชปํ๋๋ก ํจ private ํ๋
- ๊ฐ์ฒด๊ฐ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ํตํด์๋ง ์ ๊ทผ ๊ฐ๋ฅ public ๋ฉ์๋
์ฅ์
- ๊ฐ์ฒด ๋ชจ๋ํ ๊ฐ๋ฅ
- ์ฝ๋ ์ด์์ฑ ์ข์์ง
public class EncapsulationExample {
private String name;
private int age;
private String registrationNumber;
public EncapsulationExample() {}
public EncapsulationExample(String name, int age, String registrationNumber) {
this.name = name;
this.age = age;
this.registrationNumber = registrationNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.equals("")) return;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0 || age > 100) return;
this.age = age;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
if (registrationNumber == null || registrationNumber.equals("")) return;
final String REGSTRATION_REGEX = "^\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|[3][01])\\-[1-4][0-9]{6}$";
if (!registrationNumber.matches(REGSTRATION_REGEX)) return;
this.registrationNumber = registrationNumber;
}
@Override
public String toString() {
return "NoEncapsulationExample{" +
"name='" + name + '\'' +
", age=" + age +
", registrationNumber='" + registrationNumber + '\'' +
'}';
}
}
public class Main {
public static void main(String[] args) {
NoEncapsulationExample noEncapsulationExample = new NoEncapsulationExample("bab", 30, "910308-1234567");
System.out.println(noEncapsulationExample);
noEncapsulationExample.age = -10000; // ์ธ๋ถ์์ ์ง์ ์ ๊ทผํด ๋ฐ์ดํฐ๋ฅผ ์ด์ํ๊ฒ ์์ ํ ์ ์์
noEncapsulationExample.name = null; // ์ธ๋ถ์์ ์ง์ ์ ๊ทผํด ๋ฐ์ดํฐ๋ฅผ ์ด์ํ๊ฒ ์์ ํ ์ ์์
noEncapsulationExample.registrationNumber = "121234-9234567";
System.out.println(noEncapsulationExample);
System.out.println();
///////////////////////////////////////////////////
EncapsulationExample encapsulationExample = new EncapsulationExample("bab", 30, "910308-1234567");
System.out.println(encapsulationExample);
encapsulationExample.setAge(-10000);
encapsulationExample.setName(null);
encapsulationExample.setRegistrationNumber("121234-9234567");
System.out.println(encapsulationExample);
}
}
๊ตฌํ ๋ฐฉ๋ฒ
- ์ ๊ทผ์ ์ด์
์ ๊ทผ ์ ์ด์ ๊ฐ์ ํด๋์ค ๋ฉค๋ฒ ๊ฐ์ ํจํค์ง ๋ฉค๋ฒ ์์ ํด๋์ค ๋ฉค๋ฒ ๊ทธ ์ธ
public | โญ๏ธ | โญ๏ธ | โญ๏ธ | โญ๏ธ |
protected | โญ๏ธ | โญ๏ธ | โญ๏ธ | โ |
default | โญ๏ธ | โญ๏ธ | โ | โ |
private | โญ๏ธ | โ | โ | โ |
- private - ํด๋์ค ๋ด๋ถ ์ธ๋ถ ๊ตฌํํ๊ธฐ ์ํด ์ฌ์ฉ
- public - private ํ๋๊ณผ ํ๋ก๊ทธ๋จ ์ฌ์ด ์ธํฐํ์ด์ค ์ญํ
- default - ์ ๊ทผ ์ ์ด ๊ธฐ๋ณธ๊ฐ
- protectedpublic > protected > default > private
setter/getter
- ๊ฐ์ฒด ์ ๊ทผํ๋๋ฐ ํ๋๋ ์จ๊ธฐ๊ณ ๋ฉ์๋๋ฅผ ํตํด์๋ง ์ ๊ทผ ๊ฐ๋ฅํ๊ฒ ํจ
- ๋ฉ์๋ ๋ด๋ถ์์๋ ์ ๋ณด์์์ด๋ ์ ํจํ์ง ์์ ๋ฐ์ดํฐ๋ฅผ ๋ง๋ ๋ก์ง์ ๊ตฌํํ ์ ์์
- if ๋๋ regex์ ํตํด
- ๋ฉ์๋๋ฅผ ํ๋ฒ ๊ฑฐ์ณ์ ์ ํจํ ๋ฐ์ดํฐ๋ง ํ๋์ ๋ฑ๋ก๋ ์ ์๋๋ก ํจ
โญ์ ์ ํ๋์ ๋ฉ์๋
- ์๋ฏธ
- static
- ํด๋์ค์ ๊ณ ์ ๋ ๋ฉค๋ฒ → ํด๋์ค ๋ฉค๋ฒ
- ํด๋์ค ๋ก๋ฉ์ ๊ฐ์ด ๋ก๋ฉ๋์ด ๋ฉ๋ชจ๋ฆฌ ๊ณตํต์์ญ์ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๊ฐ์ฒด ์์ฑ ํ์ ์์
- ํด๋์ค ๋ก๋๊ฐ ๋ฐ์ดํธ์ฝ๋๋ก ๋ก๋ฉํด์ ๋ฉ์๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ์ ์ฌํจ
- ๊ฐ์ฒด๋ง๋ค ๊ฐ์ง๊ณ ์์ด์ผ ํ ๋ฐ์ดํฐ๋ฉด ์ธ์คํด์ค ํ๋๋ก ์ ์ธ
- ๊ฐ์ฒด๋ง๋ค ๊ฐ์ง๊ณ ์์ ํ์์๋ ๊ณต์ฉ ๋ฐ์ดํฐ๋ผ๋ฉด ์ ์ ํ๋๋ก ์ ์ธ
- ๋ฉ๋ชจ๋ฆฌ
![](https://blog.kakaocdn.net/dn/yqkVV/btrN2Wpv0xd/4TuBbcsEsDKDR9sOlHziQK/img.png)
- ์ ์ธ ๋ฐฉ๋ฒ
public class ํด๋์ค์ด๋ฆ {
// ์ ์ ํ๋
์ ๊ทผ์ ์ด์ static ํ์
ํ๋ = ์ด๊ธฐ๊ฐ;
// ์ ์ ๋ฉ์๋
์ ๊ทผ์ ์ด์ static ๋ฐํํ ๋ฉ์๋ (๋งค๊ฐ๋ณ์, ...) { ... }
}
- ์ฌ์ฉ ์์ 1๏ธโฃ
- ํ ์ฌ๋ผ์์ ๋ง๋ ์๋์ฐจ ํด๋์ค๋ฅผ ์ค๊ณํ๋ค๊ณ ๊ฐ์
- ํด๋น ์๋์ฐจ ํด๋์ค์์ ์์ฑ๋ ๊ฐ์ฒด๋ ๋ชจ๋ ๋ธ๋๋์ด๋ฆ์ ์ ์ฅํ๋ ํ๋๊ฐ ํ ์ฌ๋ผ๋ฅผ ๊ฐ์ง static
- ๋ธ๋๋์ด๋ฆ์ ๋ณ๊ฒฝ๋ ์ ์์ final
import java.util.Arrays;
import java.util.Objects;
public class Car { // ํ
์ฌ๋ผ ์๋์ฐจ๋ง ๋ง๋๋ ๊ณต์ฅ
private String ID;
private String modelName;
private String color;
private String[] options;
private static final String BRAND = "TESLA"; // ์ ์ ํ๋
public Car() {}
public Car(String ID, String modelName, String color, String[] options) {
this.ID = ID;
this.modelName = modelName;
this.color = color;
this.options = options;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
public static String getBrandName() { // ์ ์ ๋ฉ์๋
return Car.BRAND;
}
@Override
public String toString() {
return "Car{" +
"ID='" + ID + '\'' +
", modelName='" + modelName + '\'' +
", color='" + color + '\'' +
", options=" + Arrays.toString(options) +
'}';
}
}
์ ์ ๋ธ๋ก
- ๋ชฉ์
- ์ ์ ํ๋์ ๋ณต์กํ ์ด๊ธฐํ๋ฅผ ์ํจ
- ์คํ์์
- ํด๋์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ก ๋ก๋ฉ๋ ๋ ์ ์ธ๋ ์์๋๋ก ์คํ
- ๊ตฌ์กฐ
// ์ ์ ํ๋ ์ด๊ธฐํ๋ฅผ ์ํด ๊ณ์ฐ์ด ํ์ํ ๊ฒฝ์ฐ
// ์ ์ ๋ธ๋ก (static block) ์ฌ์ฉ
public class Television {
private static final String BRAND;
private static final String MODEL;
private static final String INFO;
private static int numOfTelevision = 0;
static {
BRAND = "SUMSUNG";
MODEL = "QLED";
INFO = BRAND + "-" + MODEL;
}
}
- ํน์ง
- ๊ฐ์ฒด๊ฐ ์์ด๋ ์คํ๋ ์ ์๊ธฐ ๋๋ฌธ์ this ํค์๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐๐ฏ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] PriorityQueue (0) | 2022.10.19 |
---|---|
[Java] Generic Method ์ ๋ค๋ฆญ ๋ฉ์๋ (0) | 2022.10.12 |
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค (0) | 2022.10.07 |
[Java] ๋ฐฐ์ด (6) | 2022.09.30 |
[Java] ๋ฌธ์์ด (0) | 2022.09.30 |