class Car {
// color: string;
constructor(readonly color: string) {
this.color = color;
}
start() {
console.log("start");
}
}
class Car {
name: string = "car";
color: string;
constructor(color: string) {
this.color = color;
}
start() {
console.log("start");
}
}
class Bmw extends Car {
constructor(color: string) {
super(color);
}
showName() {
console.log(super.name);
}
}
const z4 = new Bmw("black");
접근 제한자
- public
- 자식 클래스, 클래스, 클래스 인스턴스에서 접근 가능
- private
- protected
class Car {
// name과 같은 프로퍼티에 아무것도 지정하지 않으면
// 명시적으로 public
readonly name: string = "car";
// 앞에 #을 붙히면 기능상 private과 같음
#color: string;
constructor(color: string) {
this.#color = color;
}
start() {
console.log("start");
}
}
class Bmw extends Car {
constructor(color: string) {
super(color);
}
showName() {
console.log(super.name);
}
}
const z4 = new Bmw("black");
static property
class Car {
readonly name: string = "car";
#color: string;
static wheels = 4;
constructor(color: string) {
this.#color = color;
}
start() {
console.log("start");
console.log(this.name);
// static으로 선언한 변수는 클래스명으로 호출
console.log(Car.wheels);
}
}
class Bmw extends Car {
constructor(color: string) {
super(color);
}
showName() {
console.log(super.name);
}
}
const z4 = new Bmw("black");
추상 클래스
abstract class Car {
color: string;
constructor(color: string) {
this.color = color;
}
start() {
console.log("start");
}
// abstract로 선언한 변수나 함수는 이름이 있다고 알려주는 역할
// 상속받은 쪽에서 구체적으로 구현해줄 것
abstract doSomething():void;
}
// 추상 클래스는 new로 객체 생성 불가능
// const car = new Car("red");
class Bmw extends Car {
constructor(color: string) {
super(color);
}
doSomething() {
alert(3);
}
}
const z4 = new Bmw("black");