Using reflection technology to realize subtraction in Android

Source: Internet
Author: User
Tags definition reflection

Java Reflection mechanism Definition:

The Java reflection mechanism is in the running state, and for any class, all the properties and methods of the class are known, and any one of its methods can be invoked for any object, and this dynamically acquired information and the function of the method that dynamically invokes the object are called the reflection mechanism of the Java language.

The Java reflection mechanism mainly provides the following features: To determine at run time the class to which any object belongs, to construct an object of any class at run time, to determine the member variables and methods of any class at run time, to invoke a method of any object at run time, and to generate a dynamic proxy.

Sometimes we say that a language has a strong dynamic, and sometimes we distinguish between dynamic and static technologies and practices. We're catchy. Dynamic binding (dynamically binding), dynamic linking, dynamic loading, and so on. However, the term "dynamic" does not have the absolute and universally applicable strict definition, sometimes even like object-oriented was originally imported into the programming domain, one person a number, each blow each tune.

Generally speaking, the developer community speaks of dynamic languages, with a broadly agreed definition of: "When a program runs, it allows you to change the program structure or variable type, which is called a dynamic language." From this point of view, Perl,python,ruby is a dynamic language, c++,java,c# is not a dynamic language.

Although Java is not a dynamic language in this definition and classification, it has a very prominent dynamic correlation mechanism: Reflection. The meaning of this word is "reflection, image, reflection," which is used in Java to refer to the classes that we can load, detect, and use during compilation. In other words, a Java program can load a class that knows its name at run time, learns its full structure (but does not include methods definitions), generates its object entity, or sets a value on its fields, or evokes its methods1. This ability to "See Through Class" (the ability of the program to examine itself) is called introspection (introspective, internal, introspective). Reflection and introspection are two terms that are often mentioned.

The above excerpt from Baidu Encyclopedia, there are many classes in Android is closed, such as ServiceManager Bluetooth module is more than n multiple classes are hidden by Android, to invoke these classes must use Java Reflection technology to convert classes to objects to operate. Android apps are also based on the Java language and, of course, reflective technology, and I've written a demo on how to invoke the class name method through reflection technology and complete a subtraction.

First we define a class, which simply defines several methods, that is, subtraction four methods, the code is as follows:

class operationClass {
     public float add(int parm1, int parm2) {
         return parm1 + parm2;
     }
     public float cut(int parm1, int parm2) {
         return parm1 - parm2;
     }
     public float ride(int parm1, int parm2) {
         return parm1 * parm2;
     }
     public float Except(int parm1, int parm2) {
         return parm1 / parm2;
     }
}

Related Article

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.