2017-02-12 47 views
0

在ImageJ的,接口插件有一個方法運行()是這樣的:它是如何工作的,當插件被加載時調用run()方法?

package ij.plugin; 

/** Plugins that acquire images or display windows should 
    implement this interface. Plugins that process images 
    should implement the PlugInFilter interface. */ 
public interface PlugIn { 

    /** This method is called when the plugin is loaded. 
     'arg', which may be blank, is the argument specified 
     for this plugin in IJ_Props.txt. */ 
    public void run(String arg); 

} 

爲什麼run()方法可被加載插件時被自動調用?

+0

因爲這是他們如何實現的呢? –

+0

對不起,我還是不明白。假設我寫了一個名爲ImageCropper的類實現了Plugin並覆蓋了run()方法,並且我在這個方法中做了一些圖像裁剪操作,然後當ImageCropper類被加載時它會自動執行。怎麼樣 ? – Zhang

回答

3

run()方法可以在加載插件時自動調用?

沒有什麼是自動的。還有就是在ImageJ的庫一行代碼它說:

thePlugIn.run(arg); 

完整的片段是這樣的(從here):

/** Runs the specified plugin and returns a reference to it. */ 
public static Object runPlugIn(String commandName, String className, String arg) { 
    if (arg==null) arg = ""; 
    if (IJ.debugMode) 
     IJ.log("runPlugIn: "+className+argument(arg)); 
    // Load using custom classloader if this is a user 
    // plugin and we are not running as an applet 
    if (!className.startsWith("ij.") && applet==null) 
     return runUserPlugIn(commandName, className, arg, false); 
    Object thePlugIn=null; 
    try { 
     Class c = Class.forName(className); 
     thePlugIn = c.newInstance(); 
     if (thePlugIn instanceof PlugIn) 
      ((PlugIn)thePlugIn).run(arg); 
     else 
      new PlugInFilterRunner(thePlugIn, commandName, arg); 
    } 
    catch (ClassNotFoundException e) { 
     if (IJ.getApplet()==null) 
      log("Plugin or class not found: \"" + className + "\"\n(" + e+")"); 
    } 
    catch (InstantiationException e) {log("Unable to load plugin (ins)");} 
    catch (IllegalAccessException e) {log("Unable to load plugin, possibly \nbecause it is not public.");} 
    redirectErrorMessages = false; 
    return thePlugIn; 
}