你可以这样做
public class Derived extends base { public static void main(String ... args) { System.out.println(new Derived().createInstance()); }}abstract class base { public base createInstance() { //using reflection try { return getClass().asSubclass(base.class).newInstance(); } catch (Exception e) { throw new AssertionError(e); } }}版画
Derived@55fe910c
一种更常见的模式是使用Cloneable
public class Derived extends base { public static void main(String ... args) throws CloneNotSupportedException { System.out.println(new Derived().clone()); }}abstract class base implements Cloneable { @Override public Object clone() throws CloneNotSupportedException { return super.clone(); }}版画
Derived@8071a97
但是,应避免使用两者之一。通常,还有另一种方法可以满足您的需求,因此基础不会隐式地依赖于派生。



