Skip to content

Instantly share code, notes, and snippets.

@kamiazya
Last active February 26, 2020 08:36
Show Gist options
  • Save kamiazya/1f3aa2f5177ec772be05cc693e7dd527 to your computer and use it in GitHub Desktop.
Save kamiazya/1f3aa2f5177ec772be05cc693e7dd527 to your computer and use it in GitHub Desktop.
abstract class Car {
protected buildId: string;
/**
* 製造番号
*/
public getBuildId(): string {
return this.buildId;
}
abstract drive(): void;
}
class SuperCar extends Car {
private static buildNumber = 0;
public static getBuildedCarsCount(): number {
return this.buildNumber;
}
constructor(private color: string = 'red') {
super();
this.buildId = `supercar-${++SuperCar.buildNumber}`;
}
public getColor(): string {
return this.color;
}
public drive(): void {
console.log('super car drives!');
}
}
class HyperCar extends Car {
private static buildNumber = 0;
public static getBuildedCarsCount(): number {
return this.buildNumber;
}
constructor(private color: string = 'black') {
super();
this.buildId = `HYPER_CAR_${++HyperCar.buildNumber}`;
}
public getColor(): string {
return this.color;
}
public drive(): void {
console.log('SUPER CAR DRIVES!');
}
}
const superCarA = new SuperCar();
console.log(superCarA.getColor());
// red
console.log(superCarA.getBuildId());
// supercar-1
const superCarB = new SuperCar('blue');
console.log(superCarB.getColor());
// blue
console.log(superCarB.getBuildId());
// supercar-2
console.log('SuperCarは', SuperCar.getBuildedCarsCount(), '台製造されています。');
// SuperCarは 2 台製造されています。
const hyperCarC = new HyperCar();
console.log(hyperCarC.getColor());
// black
console.log(hyperCarC.getBuildId());
// HYPER_CAR_1
const hyperCarD = new HyperCar('silver');
console.log(hyperCarD.getColor());
// silver
console.log(hyperCarD.getBuildId());
// HYPER_CAR_2
console.log('HyperCarは', HyperCar.getBuildedCarsCount(), '台製造されています。');
// HyperCarは 2 台製造されています。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment