7-1/ class 연습

7-1/ class 연습

연습문제 1

아래 자바스크립트를 타입스크립트로 바꾸시오

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Car(name) {
this.name = name;
this.speed = 0;

this.honk = function() {
console.log("부우우웅");
};

this.accelerate = function(speed) {
this.speed = this.speed + speed;
}
}

var car = new Car("BENZ");
car.honk();
console.log(car.speed);
car.accelerate(10);
console.log(car.speed);

1차

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Car {
constructor(public name: string) {
}
public speed: number = 0
public honk(): void {
console.log('부우우웅')
}
public accelerate(speed) {
this.speed = this.speed + speed;
}
}

const car = new Car("BENZ");
car.honk();
console.log(car.speed);
car.accelerate(10);
console.log(car.speed);

2차

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Car {
constructor(private name: string) {
}
private _speed: number = 0
public honk(): void {
console.log('부우우웅')
}
public accelerate(speed) {
this._speed = this.speed + speed;
}

get speed(): number {
return this._speed;
}
}

const car = new Car("BENZ");
car.honk();
console.log(car.speed);
car.accelerate(10);
console.log(car.speed);

3차

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
interface ICar {
honk(): void;
accelerate(speed: number): void
}
class Car {
constructor(private name: string) {
}
private _speed: number = 0

public honk(): void {
console.log('부우우웅')
}

public accelerate(speed) {
this._speed = this._speed + speed;
}
get speed(): number {
return this._speed;
}
}

const car = new Car("BENZ");
car.honk();
console.log(car.speed);
car.accelerate(10);
console.log(car.speed);