객체 지향 입문
1. 객체 지향 프로그래밍
객체를 정의 -> 각 객체가 제공하는 기능 구현 -> 각 객체가 제공하는 기능들 간의 소통(메세지 전달)을 통하여 객체간의 협력을 구현
2. 클래스
java 파일 하나에 클래스는 여러 개 존재가능
public 클래스는 하나
3. 함수와 메서드
함수는 하나의 기능을 수행하는 코드로 여러 곳에서 동일한 방식으로 호출되어 사용될 수 있음
메서드는 객체의 기능을 구현하기 위해 클래스 내부에 구현되는 함수
4. 객체의 속성은 멤버 변수로, 객체의 기능은 메서드로 구현
학생 클래스의 속성을 멤버 변수로 선언
package ch04;
public class Student {
public int studentID;
public String studentName;
public String address;
public void showStudentInfo() {
System.out.println(studentName + "," + address);
}
public String getStudentName() {
return studentName;
}
}
학생 클래스를 생성하여 생성된 객체에 각각 이름과 주소 대입
package ch04;
public class StudentTest {
public static void main(String[] args) {
Student studentLee = new Student();
studentLee.studentName = "이순신";
studentLee.address = "서울";
studentLee.showStudentInfo();
Student studentKim = new Student();
studentKim.studentName = "김유신";
studentKim.address = "경주";
studentKim.showStudentInfo();
System.out.println(studentLee);
System.out.println(studentKim);
}
}
5. 생성자
객체를 생성하기 위해 new 키워드와 함께 사용
생성자는 외부에서 접근 가능하지만 필요에 의해 private로 선언되는 경우도 있음
6. 객체구현하기 실습
다음 설명에 해당되는 객체를 구현하고 해당정보를 출력하기
- 키가 180 이고 몸무게가 78 킬로인 남성의 이름은 Tomas 이고 나이는 37세 입니다.
package test01;
public class Person {
int height;
int weight;
String name;
int age;
public Person() {
this(180, 78, "Tomas",37);
}
public Person(int height, int weight, String name, int age) {
this.height=height;
this.weight=weight;
this.name=name;
this.age=age;
}
public void showPerson() {
System.out.println
("키가 "+height+"이고 몸무게가 "+weight+"킬로인 남성의 이름은 "+name+"이고 나이는 "+age+"세 입니다.");
}
public static void main(String[] args) {
Person person = new Person();
person.showPerson();
}
}
- 음식점 배달주문 정보 출력하기
7. 참조 자료형 변수
기본 자료형의 메모리의 크기는 클래스에 따라 다름
참조 자료형을 사용할 때는 해당 변수에 대해 생성
ex) 학생과 과목에 대한 클래스를 분리하여 사용하고 과목 클래스를 활용하여 수강한 과목들의 변수타입으로 선언
학생 | 학생 | 과목 | ||
학번 학생 이름 국어 성적 수학 성적 수강하는 과목 이름 |
-> | 학번 학생 이름 국어 과목 수학 과목 |
+ | 과목 이름 과목 점수 |
8. 접근 제어 지시자
클래스 외부에서 클래스의 멤버 변수, 메서드, 생성자를 사용할 수 있는지 여부를 지정하는 키워드
private : 같은 클래스 내부에서만 접근 가능
protected : 같은 패키지나 상속관계 클래스에서 접근 가능
public : 클래스 외부 어디서나 접근 가능
private으로 제어한 멤버 변수도 public 메서드가 제공되면 접근 가능
9. get()/set() 메서드 실습
외부로부터 변수값에 직접적으로 접근하는것을 막기 위해 사용
package ch10;
public class BirthDay {
private int day;
private int month;
private int year;
private boolean isValid;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
if(month<1||month>12) {
isValid=false;
}
else {
isValid = true;
this.month = month;
}
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public void showDate() {
if(isValid) {
System.out.println(year+"년" +month+"월" +day+"일 입니다.");
}
else {
System.out.println("유효하지 않은 날짜입니다.");
}
}
}
package ch10;
public class BirthDayTest {
public static void main(String[] args) {
BirthDay date = new BirthDay();
date.setYear(2019);
date.setMonth(12);
date.setDay(30);
date.showDate();
}
}
10. 캡슐화
정보 은닉을 활용하여 꼭 필요한 정보와 기능만 외부에 오픈
11. this
인스턴스 자신의 메모리로 생성자에서 또 다른 생성자를 호출할 때 사용, 자신의 주소를 반환
객체 지향 프로그램에서 객체간에는 협력이 이루어지며 협력을 위해 필요한 메세지를 전송하고 처리하는 기능이 구현되어야 함