Class上的Java Generic

时间:2011-12-21 07:34:55

标签: java mysql generics interface implements

如何从类中运行匿名方法。

例如,

我有一个班级

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中可以吗?

1 个答案:

答案 0 :(得分:3)

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

interface MyInterface<T> { ... }

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

其余由你决定。