Interface Typescript-基于类的接口
由于私有属性e1,以下代码生成错误。我想知道e1是否是接口I的一部分。我以为接口都是关于公共元素的。我想知道如何修复代码以使其正常工作(或者如何基于具有私有属性的类创建接口) 谢谢你的帮助 安德烈Interface Typescript-基于类的接口,interface,typescript,polymorphism,Interface,Typescript,Polymorphism,由于私有属性e1,以下代码生成错误。我想知道e1是否是接口I的一部分。我以为接口都是关于公共元素的。我想知道如何修复代码以使其正常工作(或者如何基于具有私有属性的类创建接口) 谢谢你的帮助 安德烈 在TypeScript中,可以在定义接口时“扩展”类,在此接口中,您将拥有扩展类的所有成员,包括private、public和protected 尝试这样做: var i: I; i. // <-- The IDE will show you ONLY the public members of
在TypeScript中,可以在定义接口时“扩展”类,在此接口中,您将拥有扩展类的所有成员,包括private、public和protected 尝试这样做:
var i: I;
i. // <-- The IDE will show you ONLY the public members of I, but will know about the privates
vari:i;
i、 //谢谢Gilamran提供的提示。我对代码进行了如下修改,它可以正常工作。
var i: I;
i. // <-- The IDE will show you ONLY the public members of I, but will know about the privates
class A {
constructor(private e1: string, public e2: string) {}
public displayValue(): string {
return this.e1 + ":" + this.e2;
}
}
interface I extends A {
e3: string;
displayValue2(): string;
}
var i: I;
class IA extends A implements I {
constructor(public e3: string, private e4: string, e1: string, e2: string) {
super(e1, e2);
}
public displayValue(): string {
return this.e2 + ":" + this.e3 + ":" + this.e4;
}
public displayValue2(): string {
return "testing";
}
}
var f: (a: A) => void = function(a: A) {
console.log(a);
console.log(a.displayValue());
}
var a1: A = new A("teste1", "teste2");
var a2: IA = new IA("testiae2", "testiae3", "testiae4", "testiae1");
f(a1);
f(a2);