Calling newly defined method from anonymous class

房东的猫 提交于 2019-11-26 23:13:30

问题


I instantiated an object of an anonymous class to which I added a new method.

Date date = new Date() {
    public void someMethod() {}
}

I am wondering if it is possible to call this method from outside somehow similar to:

date.someMethod();

回答1:


Good question. Answer is No. You cannot directly call date.someMethod();
Let's understand first what is this.

Date date = new Date()  { ... }; 

Above is anonymous(have no name) sub-class which is extending Date class.

When you see the code like,

    Runnable r = new Runnable() {

        public void run() {

        }

    };

It means you have defined anonymous(have no name) class which is implementing(not extending) Runnable interface.

So when you call date.someMethod() it won't be able to call because someMethod is not defined in superclass. In above case superclass is Date class. It follows simple overriding rules.

But still if you want to call someMethod then following is the step.

Fisrt way>
With reference variable 'date'
date.getClass().getMethod("someMethod").invoke(date);

Second way>
With newly created anonymous sub-class of Date class's object.

new Date() 
{
    public void someMethod() {
          System.out.println("Hello");
    }
}.someMethod();   //this should be without reference 'date'



回答2:


Basically no.

This uglyness can do it however...

Date date = new Date() {
  public Date someMethod() { 
     //do some stuff here
     return this;
  }
}.someMethod();

But aside from this, you will only be able to call that method (which does not exist in the parent class) using reflection only, like this:

date.getClass().getMethod("someMethod").invoke(date);

(try-catch left out for sake of clarity...)

But seriously, don't do this! I'd feel being hated by the person who wrote this code, if I stumbled upon this in a codebase I have to work on.




回答3:


Without using reflection you can't: the method is not part of the Date API and date is only a date as far as the compiler is concerned.

The only way you can use someMethod is by calling it on the newly created instance directly:

new Date() { public void someMethod() { } }.someMethod();



回答4:


No, that is what method-local classes are for.

 class MyDate extends Date() {
   public void someMethod() {...}
 }
 MyDate date = new MyDate();
 date.someMethod();



回答5:


In Java 10+, use var keyword

var date = new Date() {
    public void someMethod() { }
};

date.someMethod();



回答6:


I don't know why you would do this, but as written it is not possible, because Date does not declare someMethod.

However you can declare a local class inside the method, e.g:

void foo ( )
{
  class MyDate extends Date
  {
     public void someMethod( );
  }

  MyDate date = new MyDate( );

  date.someMethod( );
}

Once again, I would suggest using a normal class first, because local classes, by their nature, cannot be tested.



来源:https://stackoverflow.com/questions/15527523/calling-newly-defined-method-from-anonymous-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!