आप एक सुपर वर्ग एक उपवर्ग से एक समारोह कॉल करने के लिए चाहते हैं, स्पष्ट तरीका इस तरह से आप स्पष्ट रूप से पता विधि कहीं मौजूद है और अधिरोहित किया जाना चाहिए एक सार पैटर्न को परिभाषित करने के लिए है, एक उपclass द्वारा।
यह एक उदाहरण के रूप में है, आम तौर पर आप निर्माता के भीतर एक उप विधि कॉल नहीं है के रूप में उप उदाहरण अभी तक प्रारंभ नहीं किया गया है ... (कारण है कि आप एक "अनिर्धारित" अपने प्रश्न के उदाहरण में है)
abstract class A {
// The abstract method the subclass will have to call
protected abstract doStuff():void;
constructor(){
alert("Super class A constructed, calling now 'doStuff'")
this.doStuff();
}
}
class B extends A{
// Define here the abstract method
protected doStuff()
{
alert("Submethod called");
}
}
var b = new B();
परीक्षण करें Here
और यदि @Max की तरह आप वास्तव में हर जगह अमूर्त विधि को लागू करने से बचना चाहते हैं, तो बस इससे छुटकारा पाएं। मैं इस दृष्टिकोण की अनुशंसा नहीं करता क्योंकि आप भूल सकते हैं कि आप विधि को ओवरराइड कर रहे हैं।
abstract class A {
constructor() {
alert("Super class A constructed, calling now 'doStuff'")
this.doStuff();
}
// The fallback method the subclass will call if not overridden
protected doStuff(): void {
alert("Default doStuff");
};
}
class B extends A {
// Override doStuff()
protected doStuff() {
alert("Submethod called");
}
}
class C extends A {
// No doStuff() overriding, fallback on A.doStuff()
}
var b = new B();
var c = new C();
प्रयास करें यह Here
स्रोत
2017-06-30 14:44:17
निर्माता में आभासी विधि कॉलिंग किसी भी OOP भाषा में एक बुरा व्यवहार माना जाता है: https://google.com/search?q=virtual+method+in+constructor –