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

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

问题描述

我有一个抽象类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 ints as参数。

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;

有办法吗?

我想要的示例:

假设我定义了一个Matrix类的API。在我的问题中,Matrix不能更改其大小。

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

要创建Matrix,我需要提供其大小。

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()方法。此方法将创建一个新的矩阵与反向尺寸。更具体地说,由于它在抽象类中定义,它将使用一个接受两个int的构造函数创建一个与 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.

此外,正如你所说,你不知道任何关于实现的事情 - 所以你怎么知道它适合他们有一个构造函数需要两个整数?如果其中一个还需要String?

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天全站免登陆