是否可以使抽象方法的参数列表具有可重写的长度和类型?

发布于 2024-10-15 10:39:58 字数 556 浏览 0 评论 0原文

是否可以创建如下所示的基类:

 public abstract class baseClass
{
     public abstract void SetParameters(/*want this to be adjustable*/);
}

以便覆盖它的类可以定义所需的参数? 换句话说,我想强制覆盖该方法,但将其留给覆盖类,这里需要什么 - 所以一个可能是,

 public class derivedClass1 : baseClass
{
     public override void SetParameters(Point a, int b);
}

而另一个可能是

 public class derivedClass2 : baseClass
{
     public override void SetParameters(List<Line> a, Point b, Point c, bool e);
}

感谢您可以提供的任何帮助

Is it possible to create a base class like the following:

 public abstract class baseClass
{
     public abstract void SetParameters(/*want this to be adjustable*/);
}

so that classes that override it can define the parameters required?
In other words, I want to force the method to be overridden, but leave it up to the overriding class what is required here - so one might be

 public class derivedClass1 : baseClass
{
     public override void SetParameters(Point a, int b);
}

whereas another could be

 public class derivedClass2 : baseClass
{
     public override void SetParameters(List<Line> a, Point b, Point c, bool e);
}

?

Thanks for any help you can give

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

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

发布评论

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

评论(3

欢烬 2024-10-22 10:39:58

绝对不是——这会破坏抽象方法的一半意义——没有人能够调用它,因为他们不知道实际上编写了哪个方法签名。抽象类的全部要点是,客户端可以拥有 BaseClass 类型的引用,而无需关心实际实现的类型是什么。

如果基类能够提前预测可能涉及哪些类型,则使调用者的生活更轻松的一种可能性是具有最通用的签名(通常是具有最多参数的签名)抽象,并进行各种重载>调用为客户端未指定的参数提供默认值的通用参数。

Absolutely not - that would break half the point of having the abstract method in the first place - no-one would be able to call it, because they wouldn't know which method signature had actually been written. The whole point of an abstract class is that a client can have a reference of type BaseClass without caring about what the type of the actual implementation is.

If the base class is able to predict in advance which types might be involved, one possibility to make life easier for the caller is to have the most general signature (typically the one with the most parameters) abstract, and make various overloads which call that general one providing defaults for the parameters that the client hasn't specified.

枕梦 2024-10-22 10:39:58

如果参数都设置为相同的类型和相同的数字,我猜是可以完成的(不确定是否可以使用参数 T[],但如果可能,那么您也可以调整它)。您可以通过使用泛型来实现,就像@simonalexander2005所说,

public abstract class baseClass<T> where T : class
{
     public abstract T SetParameters(params T[] parameters);
}

由您决定派生类将使用多少个参数。但是,我不确定这是否是一个好的做法......

It can be done IF the parameters were all set to the same type and in the same number i guess (not sure if you can use params T[], but if its possible, then you can adjust it too). You can achieve by using Generics, like @simonalexander2005 said

public abstract class baseClass<T> where T : class
{
     public abstract T SetParameters(params T[] parameters);
}

its up to you to decide how many parameters the derived class will use. But, im not sure if its a good practice...

感情旳空白 2024-10-22 10:39:58

不,这是不可能的。重写方法需要与参数完全匹配——这与实现接口相同。您可能对 double-dispatch 模式感兴趣。

No, this isn't possible. An overriding method needs to match the parameters exactly—it's the same as implementing an interface. You may be interested in the double-dispatch pattern.

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