Static internal classes that bring entertainment and revenue

zhaozj2021-02-08  312

Learn this skill, you can

The use of static internal classes is added to your Java usage skills collection. Static internal classes are defined in the definition of another class and labeled a static class. I will show you an instance that uses a static internal class to add test code to another class.

Static internal classes are very simple in concept and implementation. Basically, it is to define a static class in your primary class:

Public class foo {// .... public static class test {public static void main (string [] args) {// ....}}}

It is said to add auxiliary code to your major classes, the most important point is that the static internal classes are compiled into a separate .class file, this file is independent of its external class. For example, if the external class is called Foo, and an internal class called Test, then this internal class will be compiled into the Foo $ Test.class file. The separation of the .CLASS file means you can bundle the auxiliary nested code with the main external classes. In the same source file, the internal class is indeed inside the external class. You don't have to pay any release or running overhead. Awesome! For example, if the auxiliary code is just used for debugging, you only need to post the foo.class file and leave the Foo $ Test.class file.

I use this technique mainly to prepare the external class's demo code, error debug code, and automatic authentication of the unit test implementation class behavior. (Of course, as a diligent developer, I am going to convert the test code into unit testing.)

Note that you want to execute the main () method of the foo.test class, use the following command:

% Java Foo $ TEST

If you are using the command interpreter (shell) as a reserved word, you should use the following command:

% Java Foo / $ TEST

There is also a little interesting: static internal classes can access the external class and private domains depending on the definition. This thing can be said that it is both advantageous. Because you may undermine the protection domains and private domains of the external classes in unwittime, so that it is careful! The most appropriate application of this feature is to prepare a class of white box test procedures - because this can introduce some problems that use the usual black box test (black box testing that cannot access the internal state of the object).

The XYPAIR class is very simple. It provides a fixed integer pair, (x, y). Xypair.Test classes have a main () method to make simple testing and output results for Xypair. Try to adjust the test code and core code to test various possible problems.

If you are more bold, you might want to check the Junit on the JAVA unit test. You can go to the various comments in the source code, then use the JUnit's test engine to run these test programs.

Conclusion By using static internal classes, you can add an auxiliary feature to your system to complete work such as testing, and products that are officially released will not have any adverse effects.

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

New Post(0)