1. 程式人生 > >ts-類和繼承

ts-類和繼承

world end spa nbsp moved move clas let new

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

let greeter = new Greeter("world");

class Animal {
    name:string;
    constructor(theName: string) { this.name = theName; }
    move(distanceInMeters: number 
= 0) { console.log(`${this.name} moved ${distanceInMeters}m.`); } } class Snake extends Animal{ constructor(name: string){super(name);}; move(distanceInMeters = 5){ console.log("Slithering..."); super.move(distanceInMeters); } } class Horse extends Animal { constructor(name: string) { super(name); } move(distanceInMeters
= 45) { console.log("Galloping..."); super.move(distanceInMeters); } } let sam = new Snake("Sammy the Python"); let tom: Animal = new Horse("Tommy the Palomino"); sam.move(); tom.move(34);

ts-類和繼承