1 new:
java 代码
CreateInstance instance = new CreateInstance ();
2 clone :
java 代码
构造函数不能被自动调用
public class CreateInstance implements Cloneable{
public CreateInstance getInstance() throws CloneNotSupportedException{
return (CreateInstance) this.clone();
}
}
3 newInstance
java 代码
利用java.lang.Class类的newInstance方法,则可根据Class对象的实例,建立该Class所表示的类的对象实例。
创建CreateInstace类的对象实例可以使用下面的语句(这样需要一个已经存在的对象实例)。
CreateInstance instance = CreateInstance.class.newInstance();
或者使用下面的语句(只需要存在相应的.class文件即可)
CreateInstance instance = (CreateInstance)Class.forname("com.create.instance.CreateInstance").newInstance();
如果包下不存在相应.class文件,则会抛出ClassNotFoundException。