Javascript 使用typescriptsuper()
我正在尝试用TypeScript扩展一个类。我在编译时不断收到此错误:“提供的参数与调用目标的任何签名都不匹配。”我已尝试将super调用中的artist.name属性作为super(name)引用,但不起作用 如果您有任何想法和解释,我们将不胜感激。谢谢,亚历克斯Javascript 使用typescriptsuper(),javascript,oop,typescript,Javascript,Oop,Typescript,我正在尝试用TypeScript扩展一个类。我在编译时不断收到此错误:“提供的参数与调用目标的任何签名都不匹配。”我已尝试将super调用中的artist.name属性作为super(name)引用,但不起作用 如果您有任何想法和解释,我们将不胜感激。谢谢,亚历克斯 class Artist { constructor( public name: string, public age: number, public style: string, public
class Artist {
constructor(
public name: string,
public age: number,
public style: string,
public location: string
){
console.log(`instantiated ${name}, whom is ${age} old, from ${location}, and heavily regarded in the ${style} community`);
}
}
class StreetArtist extends Artist {
constructor(
public medium: string,
public famous: boolean,
public arrested: boolean,
public art: Artist
){
super();
console.log(`instantiated ${this.name}. Are they famous? ${famous}. Are they locked up? ${arrested}`);
}
}
interface Human {
name: string,
age: number
}
function getArtist(artist: Human){
console.log(artist.name)
}
let Banksy = new Artist(
"Banksy",
40,
"Politcal Graffitti",
"England / Wolrd"
)
getArtist(Banksy);
超级调用必须提供基类的所有参数。构造函数不是继承的。注释了艺术家,因为我想这样做时不需要它
class StreetArtist extends Artist {
constructor(
name: string,
age: number,
style: string,
location: string,
public medium: string,
public famous: boolean,
public arrested: boolean,
/*public art: Artist*/
){
super(name, age, style, location);
console.log(`instantiated ${this.name}. Are they famous? ${famous}. Are they locked up? ${arrested}`);
}
}
或者,如果您打算使用art参数填充基本属性,但在这种情况下,我想实际上不需要使用public on art参数,因为这些属性将被继承,并且只存储重复的数据
class StreetArtist extends Artist {
constructor(
public medium: string,
public famous: boolean,
public arrested: boolean,
/*public */art: Artist
){
super(art.name, art.age, art.style, art.location);
console.log(`instantiated ${this.name}. Are they famous? ${famous}. Are they locked up? ${arrested}`);
}
}
如果将pubic添加到每个构造函数参数中,此参数将在child和Parents中分配。我确实打算用art:Artister填充基类。第二个解决方案可以无缝地工作。非常感谢。很高兴能帮忙@mortezaT你是对的,这意味着前四个论点没有公开。我不知道如果你把Streetarist投给艺术家,然后取一个名字,会发生什么,例如,他们会是一样的吗?它隐藏了基本产权?值得注意的是第二种解决方案。您基本上是使用相同的信息创建两个艺术家对象。来自c#world,这对我来说似乎有点不合适,但它是有效的:)对于typescript,也许我会在art参数中使用一个接口,并使用一个对象初始值设定项作为接口后面的
{name:'aoue',…}
。@CodyBugsteina据我所知。看起来有人要求它或多或少像Java规范一样隐式。