private / 접근제어자
접근제어자란?
정보은닉을 위해 제공되는 기능
접근제어자를 통해 믈래스 외부에서 직접적인 접근을 허용하지 않는 멤버를 설정하여 정보 은닉을 구체화할 수 있다
public : 다른 패키지에서도 접근 가능
default : 동일 패키지 내에서만 접근 가능
protected : 상속 받은 클래스에서 접근 가능
private : 자기 클래스에서만 접근 가능(정보은닉)
infromation hiding
정보은닉이란?
다른 클래스로부터 접근을 막는 행동 ⇒ 잘못된 데이터가 삽입됨을 방지
정보은닉은 어떻게 하는가?
private 키워드 사용(변수 대상으로)
setter/getter로 입출력 선언(메소드 대상)
윤년 계산하기
윤년이란?
4년에 한번 2월달이 29일까지 있는 경우
윤년 계산하는 법
encapsulation
캡슐화란?
변수와 클래스를 하나로 묶는 작업이다.
캡슐화의 목적?
중요한 데이터를 보호, 보전(캡슐화 = 은닉성)
getter/setter
getter/setter란?
어떤 오브젝트 혹은 변수의 값을 가져오거나 설정해 주는 역할을 하는 메소드
getter/setter 선언
getter
setter
return type은 void 혹은 값의 설정 결과를 알려줄 수 있는 type이어야 한다.
argument는 수정할 맴버변수와 같은 type이어야 한다.
이름 앞에 set을 붙이고 뒤에는 수정할 맴버변수의 이름 혹은 해당 변수를 직관적으로 표현하는 단어이어야 한다.
ex) void setLength(int length);
public int getYear() { //getter
return year;
}
public void setYear(int year) { //setter
this.year = year;
}
this란?
this는 클래스를 기반으로 생성된 인스턴스를 가르키는 참조
일반적으로 인스턴스의 자기 자신을 의미함
this. ?
필드(전역변수)와 메소드 또는 생성자의 매개변수가 동일할 때 인스턴스 필드임을 명확히 하기 위해 사용
this() ?
자기 자신의 생성자를 호출할 때 사용하는 키워드
오버로딩을 이용하여 중복된 코드를 줄일 수 있다.
this()를 사용할 때 주의사항은 생성자에서만 사용이 가능하고 반드시 호출하는 곳의 첫 번째 문장에 작성해야 한다
자기 자신의 인스턴스 주소를 가르키는 this
package ex2;
public class ThisFirst {
private int first;
private int second;
public ThisFirst(int first, int second){
this.first = first;
this.second = second;
}
//반환타입을 ThisFirst 로 설정한 getFirst 메서드에서 this를 반환
public ThisFirst getFirst() {
return this;
}
}
생성자에서 다른 생성자를 호출하는 this
package ex2;
public class ThisSecond {
private int first;
private int second;
private String third;
//초기화된 생성자를 this로 호출하여 작성
public ThisSecond(int first, int second){
this(first, second, null);
}
//생성자 초기화
public ThisSecond(int first, int second, String third){
this.first = first;
this.second = second;
this.third = third;
}
public ThisSecond() {
}
public void printThisSecond(){
System.out.println("첫번째 인자 :" + first);
System.out.println("두번째 인자 :" + second + "\n");
}
}
자기 자신을 반환하는 this
package ex2;
public class ThisThird {
//멤버변수
private int first;
private int second;
//main에서 set으로 입력 받은 값 설정
public void setFirst(int first){
this.first = first;//this키워드는 자기 자신의 메모리를 가르킴, 멤버변수의
//first와 같다.
}
public void setSecond(int second) {
this.second = second;
}
public void printThisThird(){
System.out.println("첫번째 인자 :" + first);
System.out.println("두번째 인자 :" + second);
}
}
자바의 자료형 / 참조 자료형 변수
기본 자료형과 참조 자료형 차이?
기본자료형 : 메모리가 정해져 있는 자료형
참조 자료형 : 클래스 타입으로 변수를 선언하는 자료형
package ex3;
public class Student {
int studentID;
String studentName;
//변수를 타입을 클래스 네임으로 선언
Subject korea;
Subject math;
public Student(int id, String name) {
this.studentID = id;
this.studentName = name;
this.korea = new Subject();
this.math = new Subject();
}
public void setKorean(String name, int score) {
korea.subjectName = name;
korea.score = score;
}
public void setMath(String name, int score) {
math.subjectName = name;
math.score = score;
}
public void showStudentInfo() {
int total = korea.score + math.score;
System.out.println(studentName + " 학생의 총점은 " + total + "점 입니다." );
}
}
객체 협력이란?
public class Student {
private String name;
private int grade;
private int money;
public Student(String name, int money){
this.name = name;
this.money = money;
}
public void takeBus(Bus bus){
bus.busTaken(1000);
this.money -= 1000;
}
public void takeSubway(Subway subway){
subway.subwayTaken(1200);
this.money -= 1200;
}