Java-反射基盤
9594 ワード
Java-反射基盤
1つのクラスはメンバー変数(フィールド)、メソッド、コンストラクタなどの部分から構成され、反射の概念はクラスをロードし、クラスの各構成部分を解剖することによって、クラスの各部分に対する操作を実現することである.関連するクラスは主にClass,Method,Constructor,Fieldなどである.クラスをロードする3つの一般的な方法: Class c = Class.forName(path);//pathはクラスをロードするパケットパス Class c = Person.getClass();//Personはロードするクラス Class c = Person.class;
解剖類の各構成部分
Personクラス:
1つのクラスはメンバー変数(フィールド)、メソッド、コンストラクタなどの部分から構成され、反射の概念はクラスをロードし、クラスの各構成部分を解剖することによって、クラスの各部分に対する操作を実現することである.関連するクラスは主にClass,Method,Constructor,Fieldなどである.
package com.kexin.day1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo3 {
/*
* ,
*/
@Test
public void test() throws Exception{
// public Person()
Class class1 = Class.forName("com.kexin.day1.Person");
Constructor c = class1.getConstructor(null);
Person p = (Person) c.newInstance(null);
}
@Test
public void test1() throws Exception{
// public Person(String name)
Class class1 = Class.forName("com.kexin.day1.Person");
Constructor c = class1.getConstructor(String.class);
Person p = (Person) c.newInstance("Tom");
}
@Test
public void test2() throws Exception{
// private Person(long ID)
Class class1 = Class.forName("com.kexin.day1.Person");
Constructor c = class1.getDeclaredConstructor(long.class);
c.setAccessible(true);//
Person p = (Person) c.newInstance(123456);
}
@Test
public void test3() throws Exception{
// public void Run()
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Method m = class1.getMethod("Run",null);
m.invoke(p, null);
}
@Test
public void test4() throws Exception{
// public int Run(int v)
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Method m = class1.getMethod("Run",int.class);
int n = (int) m.invoke(p, 22);
System.out.println(n);
}
@Test
public void test5() throws Exception{
// private void Eat()
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Method m = class1.getDeclaredMethod("Eat",null);
m.setAccessible(true);
m.invoke(p, null);
}
@Test
public void test6() throws Exception{
// public static void Eat(String name)
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Method m = class1.getDeclaredMethod("Eat",String.class);
m.invoke(null,"Tom");
}
@Test
public void test7() throws Exception{
//
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Field f = class1.getField("age");
Class type = f.getType();
if(type.equals(int.class))
System.out.println(f.get(p));
}
@Test
public void test8() throws Exception{
//
Person p = new Person();
Class class1 = Class.forName("com.kexin.day1.Person");
Field f = class1.getDeclaredField("name");
f.setAccessible(true);
Class type = f.getType();
if(type.equals(String.class)){
System.out.println(f.get(p));
f.set(p, "Jerry");
System.out.println(f.get(p));
}
}
}
Personクラス:
package com.kexin.day1;
public class Person {
private String name = "Tom";
public int age = 10;
public Person(){
System.out.println(name);
}
public Person(String name){
this.name = name;
System.out.println("I am "+name);
}
private Person(long ID){
System.out.println(name + "'s ID is "+ID);
}
public void Run(){
System.out.println(name + " is Running");
}
public int Run(int v){
System.out.println(name + "'s vate is "+v+"km/h");
return 1;
}
private void Eat(){
System.out.println(name + " is Eating");
}
public static void Eat(String name){
System.out.println(name + " is Eating");
}
}