Java instanceof on interface -
i have next code dealing command line options:
i have these classes:
public enum dwelltimeoptions implements idwelltimeoptions{ public dwelltimeoptions findoption(string s){ homecoming null; } } public interface idwelltimeoptions extends ioptions{ public void compare(recontoolcompare rtc) throws recontoolexception; } public interface ioptions { public ioptions findoption(string s); }
the problem firstoption variable below doesn't appear instanceof ioptions, though believe should be.
object firstoption = null; seek { firstoption = class.forname("com.nim.tools.options." + capitalize(args.get(0)) + "options"); } grab (classnotfoundexception e) { // todo auto-generated grab block e.printstacktrace(); } if(firstoption instanceof ioptions){ secondoption = ((ioptions) firstoption).findoption(args.get(1).substring(1)); } else{ recontool.logerror("unrecognized option: \"" + args.get(0) + "\""); recontool.printoutoptions(); system.exit(0); }
firstoption variable "class com.nim.tools.options.dwelltimeoptions" , dwelltimeoptions class implements idwelltimeoptions extends ioptions...so dwelltimeoptions should instance of ioptions.
actually, think realized problem. firstoption variable class object , can't used in instanceof context , can't compared interface such ioptions?
class#forname
returns instance of class<?>
, in code:
firstoption = class.forname("com.nim.tools.options." + capitalize(args.get(0)) + "options");
if argument pass in args.get(0)
i
, have ioptions
, firstoption
same class<ioption>
, not instance of object implements ioption
interface.
firstoption variable class object , can't used in instanceof context , can't compared interface such ioptions
yes, assumption right.
java interface
No comments:
Post a Comment