- Argument – 인자
- 함수 호출 시 전달되는 값
- Parameter – 매개변수
- 전달되는 인자 받아들이는 변수
- name (변수이름)
public class Test { public void test(String name) { ........} public static void main(String args[]) { new Test().test("argunment"); } // ----------------------------------------- public class Person { private String name; public Person(String name) { this.name = name; } } public static void main(String args[]) { new Person("이순신"); } } // Argument 전달되는 값, 자료정보 // Parameter 자동 생성되는 args도 Parameter, Argument 받아들이는 변수
- 객체지향 프로그래밍이란 객체들 간의 상호작용을 코드로 표현하는 것
- 이때 객체들간의 관계에 따라서 접근 할 수 있는 것과 아닌 것, 권한을 구분할 필요가 생김
클래스 내부에 선언된 데이터의 부적절한 사용으로부터 보호하기 위해서 사용
- 이것이 바로 캡슐화(encapsulation)
- 접근 제어자는 캡슐화가 가능할 수 있도록 돕는 도구
접근제어자를 사용하지 않으면 조건이 바뀔시 캡슐화 안해놓으면 열군데 다 가서 변경해야함 캡슐화가 가능하게 하려고 접근제어자를 쓰는 것 클래스 안에 있는 것들은 프라이빗으로 막아놓고 생성할 때 어캐 생겨나는지 노출해주고 개꿀띠
import java.time.LocalDateTime; class Phone { String model; String color; static int price; } public class Main { public static void main(String[] args) { Phone galaxy = new Phone(); galaxy.model = "Galaxy10"; galaxy.color = "Black"; galaxy.price = 10000; Phone iPhone = new Phone(); iPhone.model = "iPhone"; iPhone.color = "Black"; iPhone.price = 10000; String condition = "조건"; // ..... //할인 조건이 맞으면(2022년11월15일13시 이후부터 1000원 할인) LocalDateTime conditionDateTime = LocalDateTime.of(2022, 11, 15, 13, 00); if (conditionDateTime.isBefore(LocalDateTime.now())) { galaxy.price = galaxy.price - 1000; } // .......... //할인 조건이 맞으면(2022년11월15일13시 이후부터 1000원 할인) if (conditionDateTime.isAfter(LocalDateTime.now())) { iPhone.price = iPhone.price - 1000; // ............. System.out.println("가격 : " + galaxy.price); } } }
접근제어자 사용
import java.time.LocalDateTime; import java.util.concurrent.locks.Condition; public class Phone { private String model; private String color; private int price; public Phone(String model, String color, int price) { this.model = model; this.color = color; this.price = price; } // inv vs Integer, boolean vs Boolean public int getDiscountPrice(Condition condition) { if (condition.isValid()) { this.price = price - 1000; } return this.price; } } public class DateTimeCondition implements Condition { public boolean isValid() { //할인 조건이 맞으면(2022년11월15일13시 이후부터 1000원 할인) if(conditionDateTime.isAfter(LocalDateTime.now())) { return true; } // 조건이 무한 확장 가능 } } public interface Conditon { public boolean isValud(); } public class Main { public static void main(String[] args) { Phone phone = new Phone("model", "color", 10000); int price = phone.getDiscountPrice(new DateTimeCondition()); System.out.println(price); } }
위 코드에서 사용한 접근제어자 private String model; private String color; private int price;
목적성이 다른 차이가 있음
public class Plug {
public void on() {
System.out.println("Plug on");
}
public void off() {
}
}
public interface InternetOfThings {
String status();
}
public class SmartPlug extends Plug implements InternetOfThings {
@Override
.....
public String status() {
...
}
}
public class Smart냉장고 extends Plug implements InternetOfThings {
@Override
.....
public String status() {
...
}
}
SmartPlug smartPlug = new SmartPlug();
smartPlug.on();
smartPlug.off();
Plug plug=smartPlug;
plug.on();
plug.stop();
InternetOfThings iot=smartPlug;
iot=smart냉장고;
//........
iot=smartPlug;
iot=smart냉장고;
// 비즈니스 로직
....
iot.status(); // 냉장고
....
notifier = new KakaoMessage(); notifier = new SMSMessage(); notifier = new EmailMessage(); notifier.sendMessage()
- notifier
- interface sendMessage()
- 저 안에 문자, 메일, 카톡 등 다 구현 해놓고
- 1억개라고 치고 1억개 하나하나 고치는게 아니라 구현 되고 적용한 것만 고치면 됨
- 추상화
- 카카오톡 전송 - 상세 구현
- 문자 전송 - 상세 구현
- 이메일 - 상세구현