package cn.classes;
public class person {
private string name;
private int age;
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;
@override
public string tostring()
{
return "person [name=" + name + ", age=" + age + "]";
}
package cn.test;
import cn.classes.person;
public class test
{
// 这样做必须在类中有一个空构造方法
public static void main(string[] args)
class<?> c = null;
try
{
c = class.forname("cn.classes.person");
person p = (person)c.newinstance();
p.setname("xy");
p.setage(20);
system.out.println(p);
}
catch (exception e)
e.printstacktrace();
}
帮助文档参见
java.lang;
java.lang.reflect