2016-06-22 5 views
5

का उपयोग करके मैं टाइपस्क्रिप्ट में एक कक्षा का विस्तार करने की कोशिश कर रहा हूं। मैं संकलन पर यह त्रुटि प्राप्त करता रहता हूं: 'आपूर्ति पैरामीटर कॉल लक्ष्य के किसी भी हस्ताक्षर से मेल नहीं खाते हैं।' मैंने सुपर कॉल में सुपर (नाम) के रूप में कलाकार.नाम संपत्ति का संदर्भ देने का प्रयास किया है लेकिन काम नहीं कर रहा है।टाइपस्क्रिप्ट सुपर()

आपके विचारों और स्पष्टीकरणों की बहुत सराहना की जाएगी। धन्यवाद - एलेक्स।

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

या आप आधार गुण को भरने के लिए कला पैरामीटर इरादा किया था लेकिन उस मामले में मुझे लगता है कि वहाँ वास्तव में कला पैरामीटर पर सार्वजनिक उपयोग करने के लिए एक की जरूरत नहीं है के रूप में गुण विरासत में मिला होगा और यह होगा ही दुकान डुप्लिकेट डेटा।

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

में असाइन किया जाएगा, मैंने कला वर्ग के साथ बेस क्लास को पॉप्युलेट करने का इरादा किया था: कलाकार। दूसरा समाधान निर्बाध रूप से काम किया। आपका बहुत बहुत धन्यवाद। –

+0

मदद करने में सक्षम होने के लिए खुश। @mortezaT आप सही हैं कि सार्वजनिक के बिना पहले चार तर्क होने का मतलब है। मुझे नहीं पता कि क्या होगा यदि आप कलाकार के लिए स्ट्रीट आर्टिस्ट कास्ट करते हैं और उदाहरण के लिए नाम का उपयोग करते हैं, तो क्या वे वही होंगे? यह आधार संपत्ति सही छुपाता है? – mollwe