переопределение метода в Java

Shift + Alt + Enter - твой друг.

Также см. «Окно»> «Автоскрытие всех»

.
5
задан ria 9 October 2009 в 11:34
поделиться

3 ответа

To answer the question, which is specifically how overriding is implemented in the virtual machine, there's a write up available in Programming for the Java Virtual Machine (Google Books link).

The VM will look for an appropriate method definition in the referenced class, and then work its way up through the inheritance stack. Obviously at some stage various optimisations will apply.

See here for a description of the relevant bytecode instruction invokevirtual:

invokevirtual looks at the descriptor given in , and determines how many arguments the method takes (this may be zero). It pops these arguments off the operand stack. Next it pops objectref off the stack. objectref is a reference to the object whose method is being called. invokevirtual retrieves the Java class for objectref, and searches the list of methods defined by that class and then its superclasses, looking for a method called methodname, whose descriptor is descriptor.

As gustafc has highlighted below, various optimisations can apply, and no doubt the JIT will introduce further.

14
ответ дан 18 December 2019 в 13:16
поделиться
0
ответ дан 18 December 2019 в 13:16
поделиться

As long as the function (method) you wish to override is not marked as final you just simply override the method by extending the class which holds that method and then provide a method with same signature but different body.

-1
ответ дан 18 December 2019 в 13:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: