2016-06-22 19 views
5

クラスをTypeScriptで拡張しようとしています。私はコンパイル時にこのエラーを受け取ります: '提供されたパラメータが呼び出しターゲットのシグネチャと一致しません。'私はsuper(name)としてsuper.allでartist.nameプロパティを参照しようとしましたが、動作しません。TypeScriptを使用するsuper()

あなたが持っている可能性のあるアイデアや説明は非常に高く評価されます。ありがとう - アレックス。

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); 
+0

**解決策:下記の@mollweの回答をご覧ください。 –

答えて

6

スーパーコールは、基本クラスのすべてのパラメータを提供する必要があります。コンストラクタは継承されません。このようにするときには必要ないと思うので、アーティストにコメントしてください。

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}`); 
    } 
} 

それとも、基本プロパティを設定するために芸術のパラメータを意図したが、その場合には、私はプロパティが継承されるだろうし、それが唯一の店舗複製と同じように芸術のパラメータにpublicを使用する必要が本当にありません推測場合データ。

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}`); 
    } 
} 
+0

それぞれのコンストラクタ引数に公理を追加すると、この引数は親クラスだけでなく親クラスにも割り当てられます –

+0

私は基底クラスにart:Artistを設定しようと考えていました。 2番目のソリューションはシームレスに機能しました。どうもありがとうございました。 –

+0

お手伝いできれば幸いです。 @mortezaTあなたは正当な理由なしに最初の4つの引数を持つことを意図しています。 StreetArtistをArtistにアクセスしてアクセス名などをキャストするとどうなるかわかりませんが、同じになりますか?それは基本的な財産権を隠していますか? – mollwe

関連する問題