我这没出现异常啊- import java.lang.reflect.Constructor;
- public class Test {
- public static void main(String[] args) throws Exception{
- Class clazz = Person.class;
- Constructor constructor = clazz.getConstructor(String.class,int.class);
- Person p = (Person)constructor.newInstance("你好",23);
- System.out.println(p);
- }
- }
- class Person{
- String name;
- int age;
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String getName(){
- return name;
- }
- public int getAge(){
- return age;
- }
- public String toString(){
- return name+"..."+age;
- }
- }
复制代码
|
-
1.png
(10.46 KB, 下载次数: 1)
|