강의 자료
실습 코드
객체(Object)
객체 지향 프로그램과 절차 지향 프로그래밍
ex) 아침에 일어나 학교를 가는 과정
절차 지향 프로그래밍
객체 지향 프로그래밍

객체 지향 프로그램은 어떻게 구현하는가?
객체를 찾아 봅시다
클래스는 객체의 청사진(blueprint)
객체의 속성(property)은 클래스의 멤버 변수(member variable) 로 선언
학생 클래스
public class Student {
int studentNumber;
String studentName;
int majorCode;
String majorName;
int grade;
}
주문 클래스
public class Order {
int orderId;
String buyerId;
String sellerId;
int productId;
String orderDate;
}
회원 클래스
public class UserInfo {
String userId;
String userPassWord;
String userName;
String userAddress;
int phoneNumber;
}
객체 지향 프로그램을 할 때는
클래스 코딩하기
함수(function)

함수 정의하기
int add(int num1, int num2) {
int result;
result = num1 + num2;
return result;
}
함수 구현하기 예제
public class FunctionTest {
public static int addNum(int num1, int num2) {
int result;
result = num1 + num2;
return result;
}
public static void sayHello(String greeting) {
System.out.println(greeting);
}
public static int calcSum() {
int sum = 0;
int i;
for(i = 0; i<=100; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
int n1 = 10;
int n2 = 20;
int total = addNum(n1, n2);
sayHello("안녕하세요");
int num = calcSum();
System.out.println(total);
System.out.println(num);
}
}
함수 호출과 스택 메모리

메서드 (method)
객체의 기능을 구현하기 위해 클래스 내부에 구현되는 함수
멤버 함수(member function)라고도 함
메서드를 구현함으로써 객체의 기능이 구현됨
메서드의 이름은 그 객체를 사용하는 객체(클라이언트)에 맞게 짓는 것이 좋음
ex) getStudentName()
학생 클래스의 속성을 멤버 변수로 선언하고 메서드를 구현한다.
public class Student {
public int studentID;
public String studentName;
public String address;
public void showStudentInfo() {
System.out.println(studentID + "학번 학생의 이름은 " + studentName + "이고, 주소는 " + address + "입니다.");
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String name) {
studentName = name;
}
}
학생 클래스를 생성하여 생성된 객체(인스턴스)에 각각 다른 이름과 주소를 대입한다.
public class StudentTest {
public static void main(String[] args) {
Student studentLee = new Student();
studentLee.studentID = 12345;
studentLee.setStudentName("Lee");
studentLee.address = "서울 강남구";
studentLee.showStudentInfo();
Student studentKim = new Student();
studentKim.studentID = 54321;
studentKim.studentName = "Kim";
studentKim.address = "경기도 성남시";
studentKim.showStudentInfo();
}
}
