2010-03-08 45 views
0
public abstract class Master 
{ 
    public void printForAllMethodsInSubClass() 
    { 
     System.out.println ("Printing before subclass method executes"); 
      System.out.println ("Parameters for subclass method were: ...."); 
    } 
} 

public class Owner extends Master { 
    public void printSomething() { 
     System.out.println ("This printed from Owner"); 
    } 

    public int returnSomeCals() 
    { 
     return 5+5; 
    } 
} 

沒有搞亂子類的方法...是否有可能在子類的方法執行之前執行printForAllMethodsInSubClass()這是可能的java或任何其他編程語言

更新:

使用AspectJ /拼音/ Python ...等 難道還可以打印參數?上面的代碼如下格式:

public abstract class Master 
{ 
    public void printForAllMethodsInSubClass() 
    { 
     System.out.println ("Printing before subclass method executes"); 

    } 
} 

public class Owner extends Master { 
    public void printSomething (String something) { 
     System.out.println (something + " printed from Owner"); 
    } 

    public int returnSomeCals (int x, int y) 
    { 
     return x+y; 
    } 
} 
+0

只是要知道:在Struts中有一個接口(http://struts.apache.org/2.1.6/struts2-core/apidocs/com/opensymphony/xwork2/Preparable.html)。 – Trick 2010-03-08 18:41:54

回答

6

AspectJ可以爲你提供這個功能,但它是一個單獨的編譯步驟,並參與一些額外的庫。

public aspect ServerLogger { 
    pointcut printSomething(); 

    before(): printSomething() 
    { 
      (Master)(thisJoinPointStaticPart.getTarget()).printForAlMethodsInSubClass(); 
    } 
} 

Eclipse項目提供了很好的與Eclipse和Maven集成的AspectJ實現。有一大堆可用的優秀文檔,以及StackOverflow中的很多非常好的素材。

[更新]

要訪問的參數信息,您可以使用

thisJoinPoint.getSignature(); 

方法來訪問有關被調用的函數信息,如果返回的對象是MethodSignature的情況下,你可以使用簽名.getParameterNames()訪問被調用函數的參數。你必須使用一點反思才能真正理解價值觀,我認爲 - AspectJ似乎無法爲你處理這個問題。我不得不實際做一些實驗來爲你獲得一些工作代碼。

+0

我對AspectJ根本不熟悉。你能提供任何可以解釋這個問題的特定鏈接嗎? – drake 2010-03-08 18:03:10

+2

http://eclipse.org/aspectj/ – Pops 2010-03-08 18:04:32

+0

謝謝。將不得不看看aspectJ。看起來像我不知道的整個其他世界。一個班輪問題,但。 aspectJ可以用於J2EE這樣的應用程序嗎? – drake 2010-03-08 18:12:52

1

這在面向方面的編程語言中是可能的,例如AspectJ

2

要回答「其他編程語言」:這是很容易可以在Ruby中:

class Master 
    REDEFINED = [] 
    def printForAllMethodsInSubClass 
    puts 'Printing before subclass method executes' 
    end 

    def self.method_added(meth) 
    if self < Master and not Master::REDEFINED.include? meth 
     new_name = "MASTER_OVERRIDE_#{meth}".intern 
     Master::REDEFINED.push meth, new_name 
     alias_method new_name, meth 
    define_method(meth) {|*args| printForAllMethodsInSubClass; send(new_name, *args)} 
    end 
    end 
end 

你也可以將在子類中使用代理申報方法:

class Master 
    def printForAllMethodsInSubClass 
    Printing before subclass method executes 
    end 

    def self.master_method(name) 
    define_method(name) {|*args| printForAllMethodsInSubClass; yield *args} 
    end 
end 

class Owner 
    master_method(:print_something) do 
    puts "This was printed from Owner" 
    end 
end 

(這種方法也可以非常自然地翻譯成Python裝飾器。)

+0

謝謝。我很想看看函數式編程語言如何做到這一點。是否也可以打印參數值?請參閱更新 – drake 2010-03-08 19:41:47

+0

嘗試您的代碼http://pastie.org/860085,但它失敗並使用大量內存....是我的執行錯誤? – drake 2010-03-08 20:00:01

+0

不,我的錯。我不會''method_added'元編程那麼多,忘記了它的一個怪癖,沒有時間去測試。將解決。 – Chuck 2010-03-08 21:01:06

0

在Python中,您可以使用元類完成此操作,下面是一個小例子。你也許可以使其更加優雅,但它只是使點

import types 

class PrintMetaClass(type): 
    def __init__(cls, name, bases, attrs): 
    # for every member in the class 
    override = {} 
    for attr in attrs: 
     member = attrs[attr] 
     # if it's a function 
     if type(member) == types.FunctionType: 
      # we wrap it 
      def wrapped(*args, **kwargs): 
      print 'before any method' 
      return member(*args, **kwargs) 
      override[attr] = wrapped 
    super(PrintMetaClass, cls).__init__(name, bases, attrs) 
    for attr in override: 
     setattr(cls, attr, override[attr]) 

class Foo: 
    __metaclass__ = PrintMetaClass 
    def do_something(self): 
     print 2 

class Bar(Foo): 
    def do_something_else(self): 
     print 3 

在這個例子中,PrintMetaClass獲得在創建Foo類的方式和它的任何子類重新定義每個方法是一個封裝原始文件並在開始時打印給定的消息。 Bar類簡單地通過繼承Foo來接收這種類似於方面的行爲,其將__metaclass__定義爲PrintMetaClass

Metaclasess在OOP:

元類在python:

+0

還沒有涉足python。但我會試試這個。謝謝 – drake 2010-03-08 19:42:17

0

除了面向方面的編程外,還有模板方法模式,http://en.wikipedia.org/wiki/Template_method_pattern

簡而言之:父類有一個抽象方法,這個子類必須實現,這個抽象方法由父類中的一個方法調用,在這個父類中放置打印輸出或任何必要的語句。

相關問題