文件结构:
Person:
package demo2;
public class Person {
private int age;
private String name;
public Person( String name,int age) {
this.age = age;
this.name = name;
System.out.println("构造函数Person(有参数)执行");
}
public Person() {
System.out.println("构造函数Person(无参数)执行");
}
}
Main:
package Main;
public class Main {
public static void main(String[] args) throws Exception{
String classname="demo2.Person";
Class cl=Class.forName(classname);
//通过newInstance 创建一个无参数的一个Person对象 (执行无参函数)
Object obj=cl.newInstance();
}
}
运行结果: