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