Learn Java from scratch by writing simple engineering __java

Source: Internet
Author: User
Tags first row

The author himself from the beginning of contact with C language, C + +, VC + +, compiled to later contact with PHP and Java, in the use of these languages are deeply aware of these languages are the same writing ideas, the code is written in a similar way, At present, the Java language is considered a bright spot is package and reflection mechanism.

Package not only provides namespace functionality, but can also be used to differentiate code modules (C language needs to create a brush to add module files, in the engineering directory to facilitate to see the specific module files need to be saved to the same name as the brush or similar folders).

The bright spot of the Java reflection mechanism is that you can specify that any existing classes, including dynamically generated classes, get objects without having to consider other factors (which are matched by traversing all mapped objects in the virtual machine). In C + + language, only virtual subclasses (must be in the form of inheritance, and base classes combine Run-time methods to dynamically store inheriting classes) achieve pseudo reflection (because the function is not fully defined as pseudo reflection ...).

Here's a topic:

1. Create an execution file within the package and invoke it in another package class:

As you can see from the diagram, a file is created within package, and the first line of the file is the introduction of the package, the creation of additional package and the invocation of the Oper class file;

in the run file, the first row specifies the package (Running), and the second row refers to the Oper class in package (operation) .

The second invocation is to specify the package. Class name that needs to be invoked, and the advantage of this invocation is that it prevents cases with duplicate names of other package.

2. Reflection usage: Calling class functions by class name:

Firstly, the import Java.lang.reflect.Field such as reflection class and anomaly class are introduced
.
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;

Gets the specified class name
class Operat = Class.forName ("Operation.oper");

Creates an instance of the class
Object Oper = Operat.newinstance ();

Gets the specified function, writes the ADD function in the Oper class, and has 2 int parameter method
set = Operat.getmethod ("Add", int.class,int.class);

Set access permissions on Properties
set.setaccessible (true);

Call this function
System.out.println (((Operation.oper) oper). ADD (10,100));

Java in the language I have contacted, whether it is ease of use or functionality are relatively forward, the next step continues to explore the wonders of the Java language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.