在 Java 中,如何创建与“this”类型相同的新类?班级?爪哇

发布于 12-02 14:11 字数 220 浏览 3 评论 0原文

我有一个简单的场景。我想创建下面的类的一个新实例。

class A implements Fancy {
    void my() {
        Fancy b = // new X(); where X could be A or B
    }
}

当您有一个也实现了 FancyB 时。

I have a simple scenario. I'd like to create a new instance of the class below.

class A implements Fancy {
    void my() {
        Fancy b = // new X(); where X could be A or B
    }
}

When you have a B which implements Fancy as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不是你的备胎2024-12-09 14:11:47

使用 create 方法创建一个抽象类 AbstractFancy

abstract class AbstractFancy implements Fancy {
    abstract Fancy create();

    void my() {
        Fancy b = create();
        //....
    }
}

在每个类中实现 create 方法:

class A extends AbstractFancy {
    Fancy create() {
        return new A();
    }
}
class B extends AbstractFancy {
    Fancy create() {
        return new B();
    }
}

Create an abstract class AbstractFancy with a create method:

abstract class AbstractFancy implements Fancy {
    abstract Fancy create();

    void my() {
        Fancy b = create();
        //....
    }
}

In each class implement the create method:

class A extends AbstractFancy {
    Fancy create() {
        return new A();
    }
}
class B extends AbstractFancy {
    Fancy create() {
        return new B();
    }
}
蓝海2024-12-09 14:11:47

我假设您希望在基类中定义 my() ,例如 FancyAdapter ,它具有不同的子类,并且您希望它创建一个实例实际的具体子类。如果您可以假设所有实现 Fancy 的类都有一个默认(无参数)构造函数:

Fancy b = (Fancy) getClass().newInstance();

I'm assuming you want my() to be defined in a base class, like a FancyAdapter, which has different child classes, and you want it to create an instance of the actual concrete child class. If you can assume that all classes implementing Fancy have a default (no-argument) constructor:

Fancy b = (Fancy) getClass().newInstance();
听闻余生2024-12-09 14:11:47
class A implements Fancy {
    void my() {
        Fancy b = (Fancy)getClass().newInstance();
    }
}
class A implements Fancy {
    void my() {
        Fancy b = (Fancy)getClass().newInstance();
    }
}
黑色毁心梦2024-12-09 14:11:47

反思性的? (不推荐)

Fancy b = this.getClass().newInstance();

如果存在基于零参数的构造函数(隐式或显式),这将起作用。确保在该语句周围执行 try {} catch{}

其他方式:

class A implements Fancy, Cloneable {

    void my() {
        try {
            Fancy b = (Fancy) clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

或者,您可能需要一个创建新 Fancy 对象的构建器。

Reflectively? (not recommended)

Fancy b = this.getClass().newInstance();

This will work if there is a zero-argument based constructor (implicit or explicit). Make sure you do a try {} catch{} around the statement.

Other way:

class A implements Fancy, Cloneable {

    void my() {
        try {
            Fancy b = (Fancy) clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Alternatively, you would want a Builder that creates a new Fancy object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文