开发者

Overriding a class as soon as it is initialised

开发者 https://www.devze.com 2023-04-11 09:46 出处:网络
Below code is overriding navigationClick in object GenObject. Is there any other way of overriding navigationClick other than extending GenObject and implementing a method override in sub - class ?

Below code is overriding navigationClick in object GenObject. Is there any other way of overriding navigationClick other than extending GenObject and implementing a method override in sub - class ?

Is there a name for such a construct as below, where an override occurs when the class is initialised ?

GenObject go= new GenObject(){
           public boolean navigationClick(int status, int time)
            {开发者_如何学JAVA


No, in order to override a method you always have to create a subclass.

What is done in your example code is called an anonymous class, but it's really just a shortcut syntax for creating a subclass. At the bytecode level, it's a class like any other, and it will have its own .class file named something like ContainingClass$0.class


You can only override by subclassing. What you're doing is creating an anonymous class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号