在Java中,有以下几种方法来创建对象:
MyClass obj = new MyClass();
MyClass obj = MyClass.class.newInstance();
Constructor<MyClass> constructor = MyClass.class.getConstructor(String.class);
MyClass obj = constructor.newInstance("argument");
MyClass obj = new MyClass();
MyClass cloneObj = obj.clone();
Class<?> clazz = Class.forName("com.example.MyClass");
Constructor<?> constructor = clazz.getConstructor();
Object obj = constructor.newInstance();
MyClass obj = MyClassFactory.create();
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: JAVA如何做枚举值映射