2011-07-25 45 views
0

可能重複:
How to make a method be called passive before invoke a method我怎麼能說與參數的方法作爲註釋

例如:

public class Robot{ 
    public static void doSomethingBefore(){ 
     System.out.println("Do something before sayHello"); 
    } 
} 

public class Person { 
    @MethodListener(className="Robot",methodName="doSomethingBefore") 
    public void sayHello(){ 
     System.out.println("hello"); 
    } 
    public static void main(String[] args){ 
     new Person().sayHello(); 
    } 
} 

如果我喜歡這個使用註釋:

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD}) 
public @interface MethodListener { 
    public String className(); 
    public String methodName(); 
} 

輸出將是:

的sayHello 之前做一些打招呼

現在如果我想在doSomethingBefore方法的一些變化:

public class Robot{ 
    public static void doSomethingBefore(String name){ 
     System.out.println("Do something before sayHello "+name); 
    } 
} 

應該如何詮釋定義的樣子我。即MethodListener註解需要進行哪些更改? 請讓我知道... 在此先感謝

+0

不是很清楚你想要什麼 – Bozho

+0

可能的重複?明確的重複!投票結束。 –

回答