java 强制扩展类

发布于 2024-11-07 21:10:06 字数 426 浏览 0 评论 0原文

在Java中,我可以以某种方式强制扩展抽象类的类以对象作为参数来实现其构造函数吗?

像这样的东西

public abstract class Points {

    //add some abstract method to force constructor to have object.
}

public class ExtendPoints extends Points {

    /**
     * I want the abstract class to force this implementation to have
     *  a constructor with an object in it?
     * @param o
     */
    public ExtendPoints(Object o){

    }
}

In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter?

Something like

public abstract class Points {

    //add some abstract method to force constructor to have object.
}

public class ExtendPoints extends Points {

    /**
     * I want the abstract class to force this implementation to have
     *  a constructor with an object in it?
     * @param o
     */
    public ExtendPoints(Object o){

    }
}

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

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

发布评论

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

评论(6

音盲 2024-11-14 21:10:06

continue

You can use a constructor with a parameter in your abstract class (make it protected if you want to dis-allow anonymous subclasses).

public abstract class Points{
    protected Points(Something parameter){
        // do something with parameter
    }
}

Doing that, you force the implementing class to have an explicit constructor, as it must call the super constructor with one parameter.

However, you cannot force the overriding class to have a constructor with parameters. It can always fake the parameter like this:

public class ExtendPoints extends Points{
    public ExtendPoints(){
        super(something);
    }
}
蘸点软妹酱 2024-11-14 21:10:06

As said by others before, the signatue of Constructors cvannot be enforced, but you could enforce a particular set of arguments by using the AbstractFactory pattern instead. Then you can define the create methods of your factory interface to have a particular signature.

可是我不能没有你 2024-11-14 21:10:06

No Constructors aren't inherited, so each Class needs to provide its own, unless you don't specify a constructor and get the default no args constructor.

眼角的笑意。 2024-11-14 21:10:06

可能在编译时不可能,但您可以使用反射在运行时检查是否声明了所需的构造函数:

public abstract class Points {

    protected Points() {
        try {

            Constructor<? extends Points> constructor = 
                getClass().getDeclaredConstructor(Object.class);
            if (!Modifier.isPublic(constructor.getModifiers()))
                throw new NoSuchMethodError("constructor not public");

        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchMethodException ex) {
            throw (NoSuchMethodError) new NoSuchMethodError().initCause(ex);
        }
    }
}

Probably there it's not possible at compile time, but you can use reflection to check at run time if the desired constructor was declared:

public abstract class Points {

    protected Points() {
        try {

            Constructor<? extends Points> constructor = 
                getClass().getDeclaredConstructor(Object.class);
            if (!Modifier.isPublic(constructor.getModifiers()))
                throw new NoSuchMethodError("constructor not public");

        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchMethodException ex) {
            throw (NoSuchMethodError) new NoSuchMethodError().initCause(ex);
        }
    }
}
朮生 2024-11-14 21:10:06

如果您向 Points 添加 public Points(Object o) {} 构造函数,则会强制所有子类构造函数调用该超级构造函数。但是我认为没有办法确保子类使用确切的构造函数签名。

If you add a public Points(Object o) {} constructor to Points, you force any subclass constructors to call that super constructor. However I don't think there's no way of ensuring that subclasses use that exact constructor signature.

画中仙 2024-11-14 21:10:06

编辑

嗯,不,不可能强制执行带有参数的构造函数。

EDIT

Well, no, its not possible to force the implementation of a constructor with argument.

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