Java Reference
In-Depth Information
case HEX:
System.out.println("outputting EXE content in
hexadecimal");
}
}
}
ViewerEXE 's view() method demonstrates using the switch statement to switch
onanenumconstant.Forbrevity,I'velimitedthismethodtoprintingmessagestostand-
ardoutput.Also,Idon'tpresentthecorresponding ViewPNG class,whichhasasimilar
structure.
Listing4-13 presentsanapplicationthatdynamicallyloads ViewerEXE or View-
erPNG ,instantiatestheloadedclassvia newInstance() ,andinvokesthe view()
method.
Listing 4-13. Loading, instantiating, and using Viewer subclasses
class ViewerDemo
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("usage :
java
ViewerDemo
filetype");
System.err.println("example:
java
ViewerDemo
EXE");
return;
}
try
{
Class<?> clazz = Class.forName("Viewer"+args[0]);
Viewer viewer = (Viewer) clazz.newInstance();
viewer.view(null, Viewer.ViewMode.HEX);
}
catch (ClassNotFoundException cnfe)
{
Search WWH ::




Custom Search