객체 지향 프로그래밍(oop)

»

객체 지향 프로그래밍(OOP)

객체 지향 프로그래밍(Object-oriented programming) 은 절차 지향 프로그래밍과는 다르게 데이터와 기능을 한데 묶어서 처리합니다

속성과 메소드가 하나의 객체 라는 개념에 포함되며 이는 자바스크립트 object 와는 다르게 클래스(class) 라는 이름으로 부릅니다

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled.png

절차 지향 프로그래밍과 객체지향 개념 정리

OOP의 4가지 기본컨셉

class 정의

[Class PoiemaWeb](https://poiemaweb.com/es6-class)

ES5 클래스는 함수로 정의할 수 있습니다

function Car(brand, name, color) {
  // 인스턴스가 만들어질 때 실행되는 코드
}

ES6에서는 class라는 키워드를 이용해서 정의할 수 있습니다

class Car {
  constructor(brand, name, color) {
    // 인스턴스가 만들어질 때 실행되는 코드
  }
}

// constructor는 인스턴스를 생성하고 클래스 필드를 초기화하기 위한 특수한 메소드

여기서 만들어지는 함수는 객체지향 프로그래밍에서 생성자(constructor) 함수 라고 불립니다

참고로 return 값을 만들지 않습니다

new 키워드

new 키워드를 사용해 클래스의 인스턴트를 만들어낼 수 있습니다

let avante = new Car("hyundai", "avante", "black");
let mini = new Car("bmw", "mini", "white");
let beetles = new Car("volkswagen", "beetles", "red");

각각의 인스턴스는 Car라는 클래스의 고유한 속성과 메소드를 갖습니다

속성과 메소드

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%201.png

속성 먼저 보겠습니다

// ES5
function Cat(brand, name, color) {
  this.brand = brand;
  this.name = name;
  this.color = color;
}

// ES6
class Car {
  constructor(brand, name, color) {
    this.brand = brand;
    this.name = name;
    this.color = color;
  }
}

여기서 this 는 인스턴스 객체를 의미합니다

그 다음은 메소드입니다

// ES5
function Car(brand, name, color) { ... }

Car.prototype.refuel = function () {
	// 연료 공급을 구현하는 코드
}

Car.prototype.drive = function () {
	// 운전을 구현하는 코드
}

// ES6
class Car {
	constructor(brand, name, color) { ... }

	refuel() {
		// 연료 공급을 구현하는 코드
	}

	drive() {
		// 운전을 구현하는 코드
	}
}

인스턴스에서의 사용

let avante = new Car('hyundai', 'avante', 'black');
avante.color; // 'black'
avante.drive()' // 아반떼가 운전을 시작합니다

let mini = new Car('bmw', 'mini', 'white');
mini.brand; // 'bmw'
mini.refule(); // 미니에 연료를 공급합니다

extends 키워드

extends 키워드는 부모 클래스를 상속받는 자식 클래스를 정의할때 사용한다

// 부모 클래스
class Circle {
  constructor(radius) {
    this.radius = radius; // 반지름
  }

  // 원의 지름
  getDiameter() {
    return 2 * this.radius;
  }

  // 원의 둘레
  getPerimeter() {
    return 2 * Math.PI * this.radius;
  }

  // 원의 넓이
  getArea() {
    return Math.PI * Math.pow(this.radius, 2);
  }
}

// 자식 클래스
class Cylinder extends Circle {
  constructor(radius, height) {
    super(radius);
    this.height = height;
  }

  // 원통의 넓이: 부모 클래스의 getArea 메소드를 오버라이딩하였다.
  getArea() {
    // (원통의 높이 * 원의 둘레) + (2 * 원의 넓이)
    return this.height * super.getPerimeter() + 2 * super.getArea();
  }

  // 원통의 부피
  getVolume() {
    return super.getArea() * this.height;
  }
}

// 반지름이 2, 높이가 10인 원통
const cylinder = new Cylinder(2, 10);

// 원의 지름
console.log(cylinder.getDiameter()); // 4
// 원의 둘레
console.log(cylinder.getPerimeter()); // 12.566370614359172
// 원통의 넓이
console.log(cylinder.getArea()); // 150.79644737231007
// 원통의 부피
console.log(cylinder.getVolume()); // 125.66370614359172

// cylinder는 Cylinder 클래스의 인스턴스이다.
console.log(cylinder instanceof Cylinder); // true
// cylinder는 Circle 클래스의 인스턴스이다.
console.log(cylinder instanceof Circle); // true

super 키워드

super 키워드는 부모 클래스를 참조할때 또는 부모 클래스의 constructor를 호출할 때 사용합니다

부모 클래스의 생성자를 자식에서 사용할때 사용됨

// 부모 클래스
class Circle {
...
}

class Cylinder extends Circle {
  constructor(radius, height) {
    // ① super 메소드는 부모 클래스의 constructor를 호출하면서 인수를 전달한다.
    super(radius);
    this.height = height;
  }

  // 원통의 넓이: 부모 클래스의 getArea 메소드를 오버라이딩하였다.
  getArea() {
    // (원통의 높이 * 원의 둘레) + (2 * 원의 넓이)
    // ② super 키워드는 부모 클래스(Base Class)에 대한 참조
    return (this.height * super.getPerimeter()) + (2 * super.getArea());
  }

  // 원통의 부피
  getVolume() {
    // ② super 키워드는 부모 클래스(Base Class)에 대한 참조
    return super.getArea() * this.height;
  }
}

// 반지름이 2, 높이가 10인 원통
const cylinder = new Cylinder(2, 10);

prototype && constructor && this

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%202.png

ES5 방식으로 된 짤

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%203.png

이해하기 쉬우라고 나온 짤

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%204.png

클래스와 인스턴스

클래스 는 일종의 원형(original form)으로 객체를 생성하기 위한 아이디어나 청사진입니다

인스턴스 는 클래스의 사례(instance object) 입니다

클래스는 객체를 만들기 위한 생성자(constructor) 함수를 포함합니다

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%205.png

클래스와 프로토타입

Javascript는 프로토타입(원형 객체) 기반 언어 입니다

Human이라는 클래스, 인스턴스, 프로토타입의 관계 목업입니다

%E1%84%80%E1%85%A2%E1%86%A8%E1%84%8E%E1%85%A6%20%E1%84%8C%E1%85%B5%E1%84%92%E1%85%A3%E1%86%BC%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%80%E1%85%B3%E1%84%85%E1%85%A2%E1%84%86%E1%85%B5%E1%86%BC(OOP)%20e18fa96859cf474787285d0fabb106bd/Untitled%206.png

__proto__ 속성을 통해 부모 클래스의 프로토타입, 혹은 부모의 부모 클래스의 프로토타입을 탐색할 수 있습니다

프로토타입 체인

프로토타입 객체도 또 다시 상위 프로토타입 객체로부터 메소드와 속성을 상속 받을수도 있고 그 상위 프로토타입 객체도 마찬가지입니다 이것을 프로토타입 체인이라고합니다