【发布时间】:2016-02-03 00:45:27
【问题描述】:
由于私有属性 e1,以下代码会生成错误。我想知道 e1 是否是接口 I 的一部分。我认为接口是关于公共元素的。我可以知道如何修复代码以使其工作(或如何将接口基于具有私有属性的类。
感谢您的帮助,
安德烈
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;
}
class IA implements I {
constructor(public e2: string, public e3: string, private e4: string) {}
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);
}
var a1: A = new A("teste1", "teste2");
var a2: IA = new IA("testiae2", "testiae3", "testiae4");
f(a1);
f(a2);
【问题讨论】:
标签: interface typescript polymorphism