1、通过new关键字创建对象。
2、通过调用Constructor类的newInstance()方法可以创建对象。
3、通过调用Class类中的newInstance()方法创建对象:
newInstance():创建此 Class类所表示类的新对象,等效于使用new关键字调用无参构造方法创建对象,如下示例:
public class Elephant {
public static void main(String[] args) throws Exception {
Elephant elephant = Elephant.class.newInstance(); //调用无参构造方法,输出111。
System.out.println(elephant);
}
public Elephant() {
super();
System.out.println(111);
}
}
输出:
111
com.lq.vo.Elephant@52e922