JAVA- base (Class objects and reflection)

JAVA- base (Class objects and reflection)

1. (1) What is the class object?

First, java, there are two objects, one is the first instance of the object, a second Class object, type information of each class is represented by a running Class object, each object has a java.lang.Class to (with to the examples described structure of the object) of reference. Class no public constructor class, Class object class is loaded by the time the Java virtual machine and an automatic calling method defineClass configured by the class loader, it is not explicitly declared a Class object.

(2) What is a reflection?

Java is reflected in the operating state, for any class, are made known to all properties and methods of this class; for any object, are able to call any of its methods and properties; and change its properties. This is also Java to be dynamic (or quasi-dynamic, so why say it is a quasi-dynamic, because the definition of dynamic languages ​​in general is running, the program allows you to change the structure or variable type, this language is called dynamic languages. from this point of view, Perl, Python, Ruby is a dynamic language, a key property of C ++, Java, C # is not a dynamic language.) language.

2. Create an object of the process?

After we finished writing java code to be compiled .class javac file, when we call this object a java code, java calls the ClassLoader class loader will load the class file as a Class class object, member variables of this object, constructor, members of the methods are loaded up with an array of them, (the compiler prompts most is the principle method code), and then creates an instance of the object to be.

3. Obtain the Class object of way?

(1) Class.forName ( "full class name"): the bytecode file loaded into memory, the object return Class.

Used for the configuration file, the class name is defined in the configuration file, read the file, the class is loaded.

(2) the class name .class: class obtained by class name attribute.

Used for passing parameters.

(3) Object .getClass (): getclass method defined in the object class.

Multi-way acquisition target bytecode.

The same byte code file (* .class) during a program is running, it will only be loaded once, no matter which way acquired by the object is a class.

 1 package cn;
 2 
 3 import cn.entity.Persion;
 4 
 5 public class calss09 {
 6     public static void main(String[] args) throws ClassNotFoundException {
 7         Class aClass = Class.forName("cn.entity.Persion");
 8         Class bClass = Persion.class;
 9         Persion persion = new Persion();
10         Class cClass =  persion.getClass();
11         Persion persion1= new Persion();
12         Class dClass =  persion.getClass();
13         persion1.getClass();
14         System.out.println(persion==persion1);
15         System.out.println(aClass==bClass);
16         System.out.println(aClass==dClass);
17         System.out.println(aClass==cClass);
18         System.out.println(cClass==dClass);
19     }
20 }
View Code


Methods 4.Class object?

 

 1 package cn.entity;
 2 
 3 public class Persion {
 4     public String name;
 5     private int age;
 6 
 7     public Persion(String name, int age) {
 8         this.name = name;
 9         this.age = age;
10     }
11 
12     public Persion() {
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     @Override
32     public String toString() {
33         return "Persion{" +
34                 "name='" + name + '\'' +
35                 ", age=" + age +
36                 '}';
37     }
38 
39     public void eat(){
40         System.out.println("eat....");
41     }
42     public void hello(String a){
43         System.out.println("say"+  a);
44     }
45 }
View Code

在反射面前,没有什么私有共有之分。

 1 package cn;
 2 
 3 import cn.entity.Persion;
 4 
 5 import java.lang.reflect.Field;
 6 
 7 public class calss10 {
 8     public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
 9         Class p = Persion.class;
10         Field[] fields = p.getFields();
11         for (Field file:fields) {
12             System.out.println(file);
13         }
14         System.out.println("-------------------------");
15         Field field = p.getField("name");
16         Persion persion  = new Persion();
17         Object name = field.get(persion);
18         System.out.println(name);
19         field.set(persion,"zhangsan");
20         System.out.println(persion);
21         //获取所有成员变量,不考虑修饰符
22         Field[] declaredFields = p.getDeclaredFields();
23         for (Field field1:declaredFields) {
24             System.out.println(field1);
25         }
26         System.out.println("-------------------------");
27         Field field1 = p.getDeclaredField("age");
28         field1.setAccessible(true);//暴力反射
29         Persion persion1 =new Persion();
30         Object o = field1.get(persion1);
31         System.out.println(o);
32     }
33 }
View Code

运行结果:

构造方法

 1 package cn;
 2 
 3 import cn.entity.Persion;
 4 
 5 import java.lang.reflect.Constructor;
 6 import java.lang.reflect.InvocationTargetException;
 7 
 8 public class calss11 {
 9     public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
10         //构造器
11         Class persionClass = Persion.class;
12         Constructor constructor = persionClass.getConstructor(String.class, int.class);
13         System.out.println(constructor);
14         Persion persion = new Persion();
15         //用构造方法创建一个对象
16         Object asd = constructor.newInstance("asd", 10);
17         System.out.println(asd);
18         //空参构造方法创建对象
19         Object o = persionClass.newInstance();
20         System.out.println(o);
21     }
22 }
View Code

运行结果:

方法:

 1 package cn;
 2 
 3 import cn.entity.Persion;
 4 
 5 import java.lang.reflect.InvocationTargetException;
 6 import java.lang.reflect.Method;
 7 
 8 public class calss12 {
 9     public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
10         //方法
11         Class<Persion> persionClass = Persion.class;
12 
13         Method method = persionClass.getMethod("eat");
14         Persion persion = new Persion();
15         //执行方法
16         method.invoke(persion);
17         System.out.println("----------------");
18         Method hello = persionClass.getMethod("hello", String.class);
19         hello.invoke(persion,"hello");
20     }
21 }
View Code

做一个小反射框架

要求创建任意对象执行任意方法

(1)创建一个pro.properties

className=cn.it.entity.Persion
methodName=eat

(2)创建Persion类

 1 package cn.it.entity;
 2 
 3 public class Persion {
 4     public String name;
 5     private int age;
 6 
 7     public Persion(String name, int age) {
 8         this.name = name;
 9         this.age = age;
10     }
11 
12     public Persion() {
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     @Override
32     public String toString() {
33         return "Persion{" +
34                 "name='" + name + '\'' +
35                 ", age=" + age +
36                 '}';
37     }
38 
39     public void eat(){
40         System.out.println("eat....");
41     }
42     public void hello(String a){
43         System.out.println("say"+  a);
44     }
45 }
View Code

(3)反射

 1 package cn.it.domain;
 2 
 3 
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.lang.reflect.InvocationTargetException;
 7 import java.lang.reflect.Method;
 8 import java.util.Properties;
 9 
10 public class calss13 {
11     public static void main(String[] args) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
12         //获取class目录下的配置文件
13         //加载配置文件,转化为一个集合
14         Properties properties=   new Properties();
15         ClassLoader classLoader = calss13.class.getClassLoader();
16         InputStream resourceAsStream = classLoader.getResourceAsStream("cn/it/pro.properties");
17         properties.load(resourceAsStream);
18 
19         String className = properties.getProperty("className");
20         String methodName = properties.getProperty("methodName");
21         Class aClass = Class.forName(className);
22         Object o = aClass.newInstance();
23         Method eat = aClass.getMethod("eat");
24         eat.invoke(o);
25 
26     }
27 }
View Code

中间写因为路径问题一直空指针,很郁闷所以把项目样子发一下

 

Guess you like

Origin www.cnblogs.com/fan123yh/p/11100170.html