2017-02-12 51 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; 
}