Discussion on package package information

xiaoxiao2021-03-06  64

Where is the package of package? Does the folder path represent the path of the package? The path of the folder is created and compiled. According to the package information, the package information is stored in the class file here. There is an example of a test. Package mytest.test; public class test {public test () {system.out.print ( "hello");} public static void main (string [] args) {test tobj = new test (); try {class.forname ("mytest.test.testa"). NewInstance ();} catch (classnotfoundexception ex) {} catch (iLlegalaccessException ex) {} catch (instantiationException ex) {}}} This is not, this class is then written in this class, then I have written a separate Java file (using UltraEdit) package mytest .test; public class testa {public testa () {system.out.print ("/ n / nhelloa");}}} Compiled into a Class file to put it under the / mytest / class / mytest / test folder. The result is that Hello Helloa is that this class is found (if there is no packet in a separate Java file, the main program can't find this class, which means that there is a package in the Class file). My package in Testa.java is package mytest.testdir; testdir is a folder that does not exist, manually created, and put the compiled Class file in this directory. The primary class also modifies Class.Forname ("MyTest.testDir.testa"). NewInstance (); running success, Testa class is successful. The result is that the laying search is based on the file path, then according to the package information inside .class, everything will be successful. Extended, if I define an interface, then publish this interface by my customer or secondary developer to implement this interface. I have traversed the class file in the main program to get the file name, and then generate a menu based on these file names, each menu corresponds to one. Treat menu events (dynamics), class.forname ("mytest.test.testa"). NewInstance (); and convert to our published interface, using the function in the interface call. This enables the functionality of the extension (similar to the plug-in plug-in). I used to know that this feature can be implemented by DLL, and now you can use Java to be implemented. Summary: The information stored in the .class file, before loading, according to the file path, then determine the package information in the .class file, can create the plug-in manner by this feature.

转载请注明原文地址:https://www.9cbs.com/read-112236.html

New Post(0)