Class

클래스 : 어떤 ‘것’을 나타내는 필드(값)와 메서드(함수)를 가지는 객체를 만들기 위한 청사진

정의

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 1) class 정의
class Vehicle {
  drive(): void {
    console.log('yeeeees');
  }

  honk(): void {
    console.log('liverpool');
  }
}

const vehicle = new Vehicle();
vehicle.drive();
vehicle.honk();
// 출력결과
// yeeeees
// liverpool

extends

다른 클래스의 메서드를 가져온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 2) extends 는 메소드를 복사 붙여넣기 해준다.
class Vehicle {
  drive(): void {
    console.log('yeeeees');
  }

  honk(): void {
    console.log('liverpool');
  }
}

class Car extends Vehicle {
  // Car 클래스는 Vehicle이 가진 메소드들을 복사한것과 똑같다.
  drive(): void {
    // extends의 원본인 Vehicle의 drive method와 다르게 입력하면 덮어쓰여진다.
    console.log('nooooooo');
  }
}

const car = new Car();
car.drive();
car.honk();
// 출력
// yeeees ( drive 메서드를 다시 정의하면 noooooo가 출력된다!)
// liverpool

Class method modifiers

public

언제 어디서나 호출될 수 있는 메서드

private

이 클래스 내의 다른 메서드에서만 호출될 수 있는 메서드

다른 개발자가 호출하는 것을 제한하기 위해 사용한다.

내가 정의해서 정확히 알고 있기 때문에 나만 쓰도록 하기 위해서 사용!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 3) private 사용
class Vehicle {
  // public drive(): void {
  //     console.log('yeeeees');
  // }

  public honk(): void {
    console.log('liverpool');
  }
}

class Car extends Vehicle {
  private drive(): void {
    // Vehicle의 자손 클래스인 Car에서 drive를 private로 수정할 수 없다.
    console.log('nooooooo'); // 해결1 : private를 제거한다. 해결2 : 부모 클래스인 Vehicle에서 drive 메서드를 제거한다.
  }

  startDrivingProcess(): void {
    this.drive();
  }
}

const car = new Car();
car.drive();
car.honk();

protected

이 클래스 내의 다른 메서드나 자손 클래스의 다른 메서드에서만 호출될 수 있는 메서드.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 4) Protected 사용
class Vehicle {
  private honk(): void {
    // 여기를 protected로 수정하면 Car 클래스에서 honk 호출 가능
    console.log('liverpool');
  }
}

class Car extends Vehicle {
  private drive(): void {
    console.log('nooooooo');
  }

  startDrivingProcess(): void {
    this.drive();
    this.honk(); // 부모 클래스에서 honk가 private이기 때문에 호출될 수 없다. 자식 클래스에서 사용하려면 protected로 바꿔준다.
  }
}

const vehicle = new Vehicle();
vehicle.honk(); // public이 아니기 때문에 외부에서 호출할 수 없다.

fields in class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 5) Field
class Vehicle {
  color: string;
  constructor(color: string) {
    // constructor : class 안에 새 instance 생성할 때 사용
    this.color = color;
  }
}

const vehicle = new Vehicle('orange');
console.log(vehicle.color);

// 위 Vehicle 클래스 내부를 다음과 같이 줄일 수 있다.
class Vehicle {
  constructor(public color: string) {
    // 이렇게 해도 똑같음
  }
}

field inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 6) field inheritance
class Vehicle {
  constructor(public color: string) {}

  protected honk(): void {
    console.log('beep');
  }
}

class Car extends Vehicle {
  constructor(color: string) {
    super(color); // vehicle의 자식 클래스인 Car도 color를 받은 변수로 설정하기 위해 super 사용
  } // color같은 인자 대신 설정하고 싶은 값으로 넣어도 된다.

  private drive(): void {
    console.log(this.color);
  }

  startDrivingProcess(): void {
    this.drive();
    this.honk();
  }
}

const car = new Car('here'); // 'here'가 color로 지정된다.
car.startDrivingProcess(); // 'here'와 'beep'이 출력된다.

인터페이스와 클래스를 활용하여 재사용성 높은 코드를 만들 수 있다.