如何强制在抽象类的所有子类中定义构造函数 [英] How can I force a Constructor to be defined in all subclass of my abstract class

查看:34
本文介绍了如何强制在抽象类的所有子类中定义构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义抽象方法的抽象类 A.这意味着,要使类不可实例化,必须实现所有抽象方法.

I have an abstract class A that define abstract methods. This means that, for a class to be instanciable, all the abstract method have to be implemented.

我希望我的所有子类都实现一个带有 2 个整数作为参数的构造函数.

I'd like all my subclasses to implement a constructor with 2 ints as parameters.

声明构造函数违背了我的目的,因为我想要在子类中定义构造函数,而我对实现一无所知.此外,我不能将构造函数声明为抽象的;

Declaring a constructor defeats my purpose, as I want the constructor defined in subclasses and I don't know anything about the implementation. Moreover I cannot declare a constructor as being abstract;

有没有办法做到这一点?

Is there a way to do this ?

我想要的示例:

假设我正在定义 Matrix 类的 API.在我的问题中,Matrix 不能改变它们的尺寸.

Lets say that I am defining the API of a Matrix class. In my problem, Matrix cannot change their dimensions.

要创建一个矩阵,我需要提供它的大小.

For a Matrix to be created, I need to provide its size.

因此,我希望我所有的实现者都为构造函数提供大小作为参数.这个构造函数的动机是问题,而不是实现问题.只要保留方法的所有语义,实现就可以对它们做任何想做的事情.

Hence, I want all my implementors to provide the constructor with the size as a parameter. This constructor is motivated by the problem, not by an implementation concern. The implementation can do whatever it wants with these, provided that all the semantic of the methods are kept.

假设我想在我的抽象类中提供 invert() 方法的基本实现.此方法将创建一个具有 this 反转维度的新矩阵.更具体地说,因为它是在抽象类中定义的,它将创建一个与 this 相同的类的新实例,使用一个带有两个整数的构造函数.由于它不知道实例,它将使用反射 (getDefinedConstructor),我想要一种方法来保证我会得到它并且它对实现有意义.

Let's say I want to provide a basic implementation of the invert() method in my abstract class. This method will create a new matrix with this inverted dimensions. More specifically, as it is defined in the abstract class, it will create a new instance of the same class as this, using a constructor that takes two ints. As it does not know the instance it will use reflection (getDefinedConstructor) and I want a way to waranty that I'll get it and that it will be meaningfull for the implementation.

推荐答案

您不能在子类中强制使用特定的构造函数签名 - 但您可以强制它通过您的子类中的构造函数抽象类取两个整数.子类可以从无参数构造函数调用该构造函数,例如传入常量.不过,这是你能到达的最接近的地方.

You can't force a particular signature of constructor in your subclass - but you can force it to go through a constructor in your abstract class taking two integers. Subclasses could call that constructor from a parameterless constructor, passing in constants, for example. That's the closest you can come though.

此外,正如您所说,您对实现一无所知 - 那么您怎么知道他们拥有一个需要两个整数的构造函数是合适的?如果其中一个也需要字符串怎么办?或者可能对其中一个整数使用常量是有意义的.

Moreover, as you say, you don't know anything about the implementation - so how do you know that it's appropriate for them to have a constructor which requires two integers? What if one of them needs a String as well? Or possibly it makes sense for it to use a constant for one of those integers.

这里的大局是什么 - 为什么你想在你的子类上强制一个特定的构造函数签名?(正如我所说,你实际上不能这个,但如果你解释你为什么想要它,一个解决方案可能会出现.)

What's the bigger picture here - why do you want to force a particular constructor signature on your subclasses? (As I say, you can't actually do this, but if you explain why you want it, a solution might present itself.)

一种选择是为工厂提供单独的界面:

One option is to have a separate interface for a factory:

interface MyClassFactory
{
    MyClass newInstance(int x, int y);
}

那么 MyClass 的每个具体子类还需要一个工厂,它知道如何构建给定两个整数的实例.不过这并不是很方便——而且你仍然需要自己构建工厂的实例.再说一遍,这里的真实情况是什么?

Then each of your concrete subclasses of MyClass would also need a factory which knew how to build an instance given two integers. It's not terribly convenient though - and you'd still need to build instances of the factories themselves. Again, what's the real situation here?

这篇关于如何强制在抽象类的所有子类中定义构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆