-4

如何从类运行匿名方法。

例如,

我有一堂课

Class A {
   void Save(){
     //do something
   }
}


Class B {
       void Save(){
         //do something
       }
    }

当我从某个地方获取对象时:

Object something = (could be A or B both because object can accept anything.)
something.Save() //doesn't work. To solve this I could use inheritance(abstract class or Interface) but if I want to do it anonymously

就像在 VB 中一样

object Form = SomeRandomForm
Form.Save() // anonymously no need to inherit   

在java中可以吗?

4

1 回答 1

3

我不知道你的意思到底是什么(一些代码可能会有所帮助),但基本上你只是在接口上定义泛型和面板上的具体类型:

interface MyInterface<T> { ... }

class StringPanel extends JPanel implements MyInterface<String> { ... }

剩下的就看你了。

于 2011-12-21T08:15:53.780 回答