2011-12-21 244 views
-4

如何從類運行匿名方法。Java類泛型

例如,

我有一個類

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中?

回答

3

我不知道它到底是什麼,你的意思是(一些代碼可以幫助),但基本上你剛纔定義的接口上的仿製藥和麪板上的具體類型:

interface MyInterface<T> { ... } 

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

休息你決定。