객체 (Object)
의사나 행위가 미치는 대상 (사전적 의미)
구체적, 추상적 데이터의 단위 (학생, 회원, 생산, 주문, 배송)
객체지향프로그래밍 VS 절차지향프로그래밍
1) 객체지향 : 객체들의 상호작용
2) 절차지향 : 시간이나 사건의 흐름에 따른 프로그래밍
객체 찾는법
온라인 쇼핑몰에 회원 로그인을 하고 여러 판매자가 판매하고있는 제품 중 하나를 골라 주문
-> 클래스는 객체의 청사진(blueprint)
ex) 객체 order
public class Order { ㅤ int orderId; String buyerId; String sellerId; int productId; String orderDate; }
객체 내 속성 = 클래스의 멤버변수
string은 기본 자료형 아니고 클래스 (이름 주소) 변수로 클래스 사용가능
public : 어디서든 접근 가능, 1개만 가능, java파일명과 public class 명은 동일해야함
package ch01; // 소문자가 좋음 ㅤ **(대소대소)** public class Student { // 대문자가 좋음, 만약 추가된다면 StudentInfo ㅤ **(소대소)** int studentNumber ; // 멤버변수, public private 등 사용가능, 변수나 메소드는 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 address; int phoneNumber; }
ㅤ
ㅤ
ㅤ
ㅤ
ㅤ
함수와 메서드
int add(int num1, int num2) { int result; result = num1 + num2; return result; }
함수는 이름, 매개변수, 반환값, 함수몸체로 구성
반환 없는 경우 : void add() {}
ex)
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(Stirng[] args) { ㅤ int n1 = 10; int n2 = 20; ㅤ addNum(n1, n2); System.out.println(total); ㅤ sayHello (“안녕하세요”) ㅤ total = calcSum(); System.out.println(total); }
함수호출과 스택메모리
스택 : 함수가 호출될 때 지역변수들이 사용하는 메모리
함수의 수행이 끝나면 자동으로 반환되는 메모리
함수 vs 메서드
함수 : 단독모듈, 어디 속하지 않음
메서드 : 함수의 일종이지만, 클래스 안에 속해있고, 클래스 안의 변수들을 활용하는 함수
= 멤버펑션 (member function) in C++
-> 메서드를 구현함으로써 매체의 기능이 구현됨
-> 메서드의 이름은 그 객체를 사용하는 객체(클라이언트)에 맞게 짓는 것이 좋음
ex) getStudentName()
ex)
package ch04; ㅤ 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; } }해당 클래스를 사용해보자
package ch04; ㅤ 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.setStudentName(“Kim”); studentKim.address = “경기도 성남시”; ㅤ studentKim.showStudentInfo (); } }
ㅤ
ㅤ
ㅤ
ㅤ
ㅤ
인스턴스
• 클래스는 객체의 속성을 정의 하고, 기능을 구현하여 만들어 놓은 코드 상태
• 실제 클래스 기반으로 생성된 객체(인스턴스)는 각각 다른 멤버 변수 값을 가지게 됨
-> 가령, 학생의 클래스에서 생성된 각각의 인스턴스는 각각 다른 이름, 학번, 학년등의 값을 가지게 됨
• new 키워드를 사용하여 인스턴스 생성
힙 메모리
• 생성된 인스턴스는 동적 메모리(heap memory) 에 할당됨
• C나 C++ 언어에서는 사용한 동적 메모리를 프로그래머가 해제 시켜야 함 ( free() 난 delete 이용) *참고_ C++ 은 new / C 는 malloc (동적 메모리)로 생성
• 자바에서 Garbage Collector 가 주기 적으로 사용하지 않는 메모리를 수거
• 하나의 클래스로 부터 여러개의 인스턴스가 생성되고 각각 다른 메모리 주소를 가지게 됨
Student studentKim = new Student(); // 인스턴스 studentKim.studentID = 54321; studentKim.setStudentName(“Kim”); studentKim.address = “경기도 성남시”; ㅤ studentKim.showStudentInfo ();
studentLee 는 중괄호{} 안에 선언 / 자료형 (type) Student / 변수 studentLee / new Student로 값을 할당
system.out.println(studentKim); -> 값 : ch04.Student@36sdq23~ : 주소가 뒤에 숫자와 알파ㅤ벳 ㅤ JBM이 assign heap메모리에 할당
studentLee : 참조 변수 (reference variable)
메모리 어드레스 (주소, 36어쩌구) : 참조 값 (reference value)
용어정리 (교재)
객체 : 객체 지향 프로그램의 대상, 생성된 인스턴스
클래스 : 객체를 프로그래밍 하기위해 코드로 정의해 놓은 상태
인스턴스 : new 키워드를 사용하여 클래스를 메모리에 생성한 상태
멤버 변수 : 클래스의 속성, 특성
메서드 : 멤버 변수를 이용하여 클래스의 기능을 구현한 함수
참조 변수 : 메모리에 생성된 인스턴스를 가리키는 변수
참조 값 : 생성된 인스턴스의 메모리 주소 값
용어정리 (인터넷)
1) 클래스와 객체
2) 객체와 인스턴스
3) 객체의 구성요소
4) 인스턴스의 생성
5) 생성자(Constructor)
출처: https://gangzzang.tistory.com/entry/클래스Class-객체Object-인스턴스Instance-생성자Constructor [갱짱.study]

본 게시글은 fastcampus 박은종강사 수업을 듣고 개인적으로 정리한 내용임을 밝힘.