获取类
TestClass.java
1 2 3
| package org.example;
public class TestClass {}
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package org.example;
public class Main { public static void main(String[] args) throws ClassNotFoundException {
Class c1 = TestClass.class; System.out.println(c1.getName()); System.out.println(c1.getSimpleName());
Class c2 = Class.forName("org.example.TestClass"); System.out.println(c1 == c2);
TestClass test = new TestClass(); Class c3 = test.getClass(); System.out.println(c3 == c2); } }
|
获取类的构造器
TestClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package org.example;
import org.junit.Test;
public class TestClass { private String name; private int age;
public TestClass() { }
public TestClass(String name, int age) { this.name = name; this.age = age; }
private TestClass(String name){ this.name = name; } }
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package org.example;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class Main { @Test public void testGetConstructors() { Class c = TestClass.class;
Constructor[] constructors = c.getDeclaredConstructors(); for (Constructor constructor: constructors){ System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); }
}
@Test public void testGetConstructor() throws Exception { Class c = TestClass.class; Constructor constructor = c.getConstructor(); System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); constructor = c.getDeclaredConstructor(); System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); constructor = c.getConstructor(String.class, int.class); System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); constructor = c.getDeclaredConstructor(String.class); System.out.println(constructor.getName() + "--->" + constructor.getParameterCount()); } }
|
类构造器作用:初始化对象返回
TestClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package org.example;
import org.junit.Test;
public class TestClass { private String name; private int age;
public TestClass() { }
public TestClass(String name, int age) { this.name = name; this.age = age; System.out.println(this.name + " " + this.age); }
private TestClass(String name){ this.name = name; System.out.println(this.name); } }
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package org.example;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class Main { @Test public void testGetConstructor() throws Exception { Class c = TestClass.class; Constructor constructor1 = c.getConstructor(String.class, int.class); System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount()); TestClass test =(TestClass) constructor1.newInstance("test", 10);
Constructor constructor2 = c.getDeclaredConstructor(String.class); System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount()); constructor2.setAccessible(true); TestClass test1 =(TestClass) constructor2.newInstance("test"); test.setAge(10); test.setName("test"); System.out.println(test1); } }
|
获取类的成员变量
TestClass.java
1 2 3 4 5 6 7 8 9 10
| package org.example;
import org.junit.Test;
public class TestClass { public static int o; public static final String test = "test-test"; private String name; private int age; }
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package org.example;
import org.junit.Test;
import java.lang.reflect.Field;
public class Main { @Test public void testGetField() throws Exception{ Class c = TestClass.class; Field[] fields = c.getDeclaredFields(); for (Field field: fields){ System.out.println(field.getName() + "--->" + field.getType()); } Field fieldName = c.getDeclaredField("name"); System.out.println(fieldName.getName() + "--->" + fieldName.getType()); Field fieldAge = c.getDeclaredField("age"); System.out.println(fieldAge.getName() + "--->" + fieldAge.getType()); TestClass test = new TestClass(); fieldName.setAccessible(true); fieldName.set(test, "test"); System.out.println(test); String name = (String) fieldName.get(test); System.out.println(name); } }
|
获取类的成员方法
TestClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package org.example;
import org.junit.Test;
public class TestClass { public static int o; public static final String test = "test-test"; private String name; private int age;
public TestClass() { System.out.println("无参构造..."); }
public TestClass(String name, int age) { this.name = name; this.age = age; System.out.println(this.name + " " + this.age); }
private TestClass(String name){ this.name = name; System.out.println(this.name); }
private void sleep(){ System.out.println("sleeping..."); }
private void sleep(String name){ System.out.println(name + " sleeping..."); }
public void eat(String name){ System.out.println(name + " eating"); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } }
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package org.example;
import org.junit.Test;
import java.lang.reflect.Field; import java.lang.reflect.Method;
public class Main { @Test public void testGetField() throws Exception{ Class c = TestClass.class; Method[] methods = c.getDeclaredMethods(); for (Method method: methods){ System.out.println(method.getName() + "--->" + method.getParameterCount() + "--->" + method.getReturnType()); } Method method1 = c.getDeclaredMethod("sleep", String.class); System.out.println(method1.getName() + "--->" + method1.getParameterCount() + "--->" + method1.getReturnType()); Method method2 = c.getDeclaredMethod("sleep"); System.out.println(method2.getName() + "--->" + method2.getParameterCount() + "--->" + method2.getReturnType()); TestClass test = new TestClass(); method2.setAccessible(true); method2.invoke(test);
} }
|
如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !