java-based reflection and nine anonymous inner classes

Reflecting frame design is the soul
(Prerequisites The: obtained must be representative of the Class bytecode, Class class is used to represent the .class files (bytecodes))

A reflective overview

class is the root of all reflective, JAVA reflection mechanism is in the operating state, for any and a class, by reflection can know all the properties and methods of this class; for any object, are able to call any of its methods and properties ; acquired reflection information, and this dynamic function call object moving method called java language
in order to anatomy of a class, must first obtain the file object class bytecode. Class is the method used by the anatomy class. Class so first acquired each object type corresponding to the byte code file.
I is simple to understand and Methods class class directly transferred by a string.

Reflex action

By accessing java object reflective properties, methods, and other construction methods

Reflection mechanism Class

Java classes associated with the reflection as follows:

The class name use
Class class On behalf of the class of entities in a Java application running represent classes and interfaces
Field class On behalf of the class member variables (also known as member variables of the class attribute)
Method class Method represents a class of
Constructor class Representative class constructor

Note: subclass of Object class

The basic mechanism of reflection

: Possible "full class name string" According to one, the representation of the class of the class object, then constructs an instance of a real object of this class according to the class object
available Class.forName ( "class package name") method to give a Class class

String className = "cn.edu360.javase24.reflect.demo.SerivceOne";  // 通常这个字符串常常存在文件当中,用io流调用
Class<?> forName = Class.forName(className);   // 根据类名字符串“SerivceOne”获取class对象
SerivceOne o = (SerivceOne)forName.newInstance();  // 用class对象构造出这个类SerivceOne的实例对象
o.say();

Then the reflection is often used in interfaces
such as here we have an interface Fuck as well as its implementation class FuckImpl, then agreed to complete the implementation class package name good save them in a file.
Then we call this interface when you can so

public class Menu {
	
public static void main(String[] args) throws Exception {

	// 从约定的文件中读取所需类的实现类全名
	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("f:/a.txt")));
	String ClassName = br.readLine();   /将文件中的按行包名输出
	
	Class<?> forName = Class.forName(ClassName); // forName代表的是serviceClassName字符串所指定的类的类class模板
	
	// 根据类全名构造这个service实现类的实例
	Fuck fuck = (Fuck) forName.newInstance();`}}

Third, the class constructor acquired by reflection, properties and methods

Class<?> forName = Class.forName(ClassName);
	
	// 根据类全名构造这个service实现类的实例
	Fuck fuck = (Fuck) forName.newInstance();`
	//methodName存的是方法名的字符串,从forName这个class模板中获取到指定的方法
	Method method=forName.getMethod(methodName) ;
	//让Method在对象上执行,()中写对象和参数
	Object invoke=method.invoke(fuck,12);

-------------------------------------------------------------------------
//如果存在同名方法,则有参数的需要这样输入
   	Method method2=forName.getMethod(methodName,String.Class) ;
   	Object invoke=method.invoke(fuck,12);

The configuration file uses gadgets Properties ()

The main method of Properties

(1)load(InputStream inStream)

This method may correspond to the attribute from the file input stream .properties, load class object property list to the Properties. The following code:

Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)

This method saves the list of attributes Properties class object to the output stream. The following code:

FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

If comments are not empty, save the file attributes first line is #comments, represents the comment information; if no comment is empty.

Annotation information is behind the current time save property file.

(3)getProperty/setProperty

These two methods are respectively get and set property information.

Loading and reflection properties of the profiles

Properties props = new Properties();
//加载xx.properties属性配置文件
props.load(Test.class.getClassLoader().getResourceAsStream("xx.properties"));
//取配置参数
String value = props.getProperty("key");
	
	Class<?> forName = Class.forName(value); // forName代表的是serviceClassName字符串所指定的类的类class模板
	
	// 根据类全名构造这个service实现类的实例
	Fuck fuck = (Fuck) forName.newInstance();`

Then should establish *** file *** directly with a package
content is
in the form of key = value

ClassName=fuckMan
Method=fuck

Anonymous inner classes

Anonymous inner classes is the name of the internal class

Because there is no name, so anonymous inner classes can only be used once, it is usually used to simplify writing code

However, the use of anonymous inner classes there is a prerequisite: you must inherit from a parent class or implement an interface
Example 1: do not use anonymous inner classes to implement abstract methods

abstract class Person {
    public abstract void eat();
}
 
class Child extends Person {
    public void eat() {
        System.out.println("eat something");
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Child();
        p.eat();
    }
}

Operating results: eat something

You can see that we inherited with Child Person class, and then implement an instance of the Child, which upcast reference to the Person class

However, if the Child class here is used only once, then it is written as a separate category would not be too much trouble?

This time on the introduction of anonymous inner classes

Example 1: do not use anonymous inner classes to implement the abstract methods

abstract class Person {
    public abstract void eat();
}
 
class Child extends Person {
    public void eat() {
        System.out.println("eat something");
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Child();
        p.eat();
    }
}

Operating results: eat something
you can see, we inherited Child Person class, and then implement an instance of the Child, which upcast reference to the Person class

However, if the Child class here is used only once, then it is written as a separate category would not be too much trouble?

This time on the introduction of an anonymous inner class
Example 2: basically anonymous inner class

abstract class Person {
    public abstract void eat();
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}

Operating results: eat something
you can see, we direct method in abstract class Person implements in braces

So that you can omit a writing class

Further, the anonymous inner classes can also be used on the interface

Example 3: Use anonymous inner classes on the interface

interface Person {
    public void eat();
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}

Operating results: eat something

Published 44 original articles · won praise 0 · Views 874

Guess you like

Origin blog.csdn.net/heartless_killer/article/details/98383206