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;
}
因爲這是他們如何實現的呢? –
對不起,我還是不明白。假設我寫了一個名爲ImageCropper的類實現了Plugin並覆蓋了run()方法,並且我在這個方法中做了一些圖像裁剪操作,然後當ImageCropper類被加載時它會自動執行。怎麼樣 ? – Zhang