class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
sayHi() {
return `My name is ${this.name}`;
}
}
class Cat extends Animal{
constructor(name: string) {
super(name); // 呼叫父類別的 constructor(name)
console.log(this.name);
}
sayHi() {
return 'Meow, ' + super.sayHi(); // 呼叫父類別的 sayHi()
}
sayMeow(){
return 'Meow~';
}
}
let c = new Cat('Tom'); // Tom
console.log(c.sayHi()); // Meow, My name is Tom
class Cat extends Animal implements cat{
constructor(name: string) {
super(name); // 呼叫父類別的 constructor(name)
console.log(this.name);
}
sayHi() {
return 'Meow, ' + super.sayHi(); // 呼叫父類別的 sayHi()
}
sayMeow(){
return 'Meow~';
}
}
let c = new Cat('Tom'); // Tom
console.log(c.sayHi()); // Meow, My name is Tom
interface Meow{
sayMeow(): any;
}
interface Hi{
sayHi() :any;
}
class Cat extends Animal implements Meow, Hi{
constructor(name: string) {
super(name); // 呼叫父類別的 constructor(name)
console.log(this.name);
}
sayHi() {
return 'Meow, ' + super.sayHi(); // 呼叫父類別的 sayHi()
}
sayMeow(){
return 'Meow~';
}
}
let c = new Cat('Tom'); // Tom
console.log(c.sayHi()); // Meow, My name is Tom
class Point {
x: number;
y: number;
}
interface Point3d extends Point {
z: number;
}
let point3d: Point3d = {x: 1, y: 2, z: 3};
error: TS2564 [ERROR]: Property 'x' has no initializer and is not definitely assigned in the constructor.
x: number;
^
TS2564 [ERROR]: Property 'y' has no initializer and is not definitely assigned in the constructor.
y: number;
class Point {
x: number;
y: number;
constructor(x: number,y:number){
this.x = x;
this.y = y;
}
}
interface Point3d extends Point {
z: number;
}
let point3d: Point3d = {x: 1, y: 2, z: 3};
abstract class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
sayHi() {
return `My name is ${this.name}`;
}
}
abstract class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
sayHi() {
return `My name is ${this.name}`;
}
}
class Cat extends Animal{
constructor(name: string) {
super(name); // 呼叫父類別的 constructor(name)
console.log(this.name);
}
sayHi() {
return 'Meow, ' + super.sayHi(); // 呼叫父類別的 sayHi()
}
sayMeow(){
return 'Meow~';
}
}
let c = new Cat('Tom'); // Tom
console.log(c.sayHi()); // Meow, My name is Tom