컴퓨터/정보처리기사 SW 공학

| 니앙팽이 - 객체지향(OOP) | 2-1 | Class & Instance

객체지향

📕 2. 객체지향 개발


📄 1. Class & Instance

1). new를 통해 Instance 생성

new 연산자로 인스턴스(객체)를 만든다.

  • (메모리 heap영역에) 데이터 저장 공간을 할당받고
    -> 그 공간의 참조값(해시코드)를 객체에게 반환해준다.
    -> 이어서 생성자를 호출한다.

2). 예시

class Calculator //클래스(공장)
{
  int left, right;
  public void setOprands(int left, int right){
    this.left = left;
    this.right = right;
  }
  public void sum(){System.out.println(this.left + this.right);}
  public void avg(){System.out.println((this.left + this.right)/2);}
}

public static void main(String[]args){
  Calculator c1 = new Calculator(); 
    //인스턴스(제품)
  c1.메소드();
}