About Monkey 2 › Forums › Monkey 2 Programming Help › Override question
This topic contains 5 replies, has 4 voices, and was last updated by
Xaron
2 years, 3 months ago.
-
AuthorPosts
-
January 3, 2017 at 3:16 pm #6174
Just a simple question. When overriding a method A the method A in the base class is NOT called right? So when I want to call the base method first I have to call Super.A() ?
Is that true for the constructor (New()) as well?
Thanks!January 3, 2017 at 4:13 pm #6178Thought you could only override an abstract (which is a shame!)
extended new calls base new…
[/crayon]Monkey123456789101112131415161718192021222324252627[crayon-5cba13f31e9a3571405076 inline="true" ]Class baseMethod New()Print "base new"End MethodMethod amethod()Print "base method"End MethodEnd ClassClass extended Extends baseMethod New()Print "extended new"End MethodEnd ClassFunction Main()Local b:base = New base()Print "-----"Local e:extended = New extended()Print "-----"b.amethod()Print "-----"e.amethod()Print "-----"End FunctionJanuary 3, 2017 at 7:12 pm #6181Thanks. I’ve extended your example. All in all base constructors are called while base methods are not (no matter if I use virtual/override for New()).
Monkey1234567891011121314151617181920212223242526272829303132Class baseMethod New()Print "base new"End MethodMethod amethod() VirtualPrint "base method"End MethodEnd ClassClass extended Extends baseMethod New()Print "extended new"End MethodMethod amethod() OverridePrint "extended method"End MethodEnd ClassFunction Main()Local b:base = New base()Print "-----"Local e:extended = New extended()Print "-----"b.amethod()Print "-----"e.amethod()Print "-----"End FunctionResults:
base new
—–
base new
extended new
—–
base method
—–
extended method
—–January 3, 2017 at 8:57 pm #6187For me it makes sense because the default constructor is the initialization. And every base ‘typed’ objects should be initialized properly..
But sometimes it not logical, in the following example the new(i:int) from base does not call default constructor but new(i:int) from extended does…
Dunno what to think about this[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142[crayon-5cba13f32621e419861066 inline="true" ]Class baseMethod New()Print "base new"'general initilisation for all base objectsEnd MethodMethod New(i:int)' virtual not necessary with new !?Print "base new"+iEnd MethodMethod amethod() VirtualPrint "base method"End MethodEnd ClassClass extended Extends baseMethod New() overridePrint "extended new"End MethodMethod New(i:int) overridePrint "extended new"+iEnd MethodMethod amethod() OverridePrint "extended method"End MethodEnd ClassFunction Main()Local b:base = New base(7)Print "-----"Local e:extended = New extended(1)Print "-----"Local b2:base = New base()Print "-----"Local e2:extended = New extended()Print "-----"b.amethod()Print "-----"e.amethod()Print "-----"End FunctionJanuary 5, 2017 at 6:33 am #6223When overriding a method, yes, you need to call Super.Blah or it’ll never be called.
When writing a constructor, you can user Super.New(…) to explicitly call a super class constructor, in which case the call to Super.New must be the first statement in the constructor.
If your constructor does not call Super.New(…) the super class must have a ‘default’ constructor – ie: a constructor with no params – and this will be called for you before your constructor runs. Note that a default ‘NOP’ constructor will be automatically generated for any class with NO constructors.
In monkey2, you can also use Self.New instead of Super.New at the start of a constructor. This will call a constructor in the same class instead of a super class constructor. A constructor can only call Self.New or Super.New but not both.
January 5, 2017 at 7:16 am #6224Thanks for clarification, Mark!
-
AuthorPosts
You must be logged in to reply to this topic.