重写方法中是否可以有专门的参数?

发布于 2024-07-19 01:52:48 字数 594 浏览 7 评论 0原文

假设我有一个名为“shape”的抽象父类,并且有多个子类(三角形、正方形、圆形...)。 我想在父“shape”类中定义一个所有子类都必须实现的抽象方法,我们称之为“draw”。 因此所有形状子类都必须提供“draw()”方法。 但是,draw 方法采用“Stencil”类型的参数,并且并非每个形状子类都可以使用任何模板...

因此存在一个抽象“shape”类、多个形状子类和多个模板。 我需要在形状类中定义一个绘制方法。 正方形可能使用 Stencil1,圆形可能使用 Stencil2。

我猜泛型可以解决这个问题,但我不确定。 每个形状子类都需要使用特定的模板来定义绘制方法,因为这些类也被其他类使用,并且编译器应该强制所有程序员使用该类支持的模板来调用绘制方法。 我们不能定义像“public abstract void draw(Stencil s)”这样的抽象方法,因为这样程序员可以将任何模板传递给 square 类,而 square 类只支持“Stencil1”

有什么想法吗?

更新1: 应该补充的是,形状类并不关心子类使用哪个模板,但由于子类也在其他类中使用,因此定义绘制方法非常重要,以便编译器只接受支持的模板。

Let's say I have an abstract parent class called "shape", and that there are multiple subclasses (triangle, square, circle... ). I want to define an abstract method in the parent "shape" class which all subclasses must implement, let's call it "draw". So all shape subclasses must provide the "draw()" method. But, the draw method takes a parameter of type "Stencil", and, not every shape subclass can use just any stencil...

So there is one abstract "shape" class, multiple shape subclasses, and multiple stencils. I need a draw method defined in the shape class. A square might use Stencil1 and the circle might use Stencil2.

I'm guessing that generics would do the trick, but I'm not sure. Each shape subclass needs to define the draw method with a specific stencil because these classes are used by other classes as well, and the compiler should force all programmers to call the draw methods with the stencil that is supported by that class. We can't define an abstract method like "public abstract void draw(Stencil s)" because then the programmer could pass in any stencil to the square class, whereas the square class only supports "Stencil1"

Any ideas?

Update1:
Should add that the shape class doesn't care which stencil is used by the subclass, but since the subclasses are used in other classes too, it's important that the draw method is defined so that only the supported stencil is accepted by the compiler.

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

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

发布评论

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

评论(4

つ低調成傷 2024-07-26 01:52:49

我认为你应该重新考虑你最初的设计。

当然,您可以通过使用instanceof等来解决这个问题。但是,这将导致一个非常混乱的API(如果这就是您使用它的目的)。

I think you should probably reconsider your initial design.

Of course, you could get around this by using instanceof, etc. However, this will result in a very confusing API (if that's what you are using it for).

夜司空 2024-07-26 01:52:49

如果您希望在编译时捕获此问题,请考虑以下选项:

  • 创建一组抽象模板。 特别是如果您认为可以将它们分组。 因此,如果您有多个“方形”模板,请创建一个 SquareStencil 抽象类型并从中派生具体实例。 您甚至可以将它们创建为抽象 Stencils 类的子集。
  • 重载绘制方法。 它没有必要是通用的。 让编译器帮助您选择适合工作的正确方法。

If you want this to be caught at compile time, the following options come to mind:

  • Create a set of abstract stencils. Particularly if you think you can group them. So if you'll have multiple "square" stencils, create a SquareStencil abstract type and derive the concrete instances from that. You can even create them as a subset of the abstract Stencils class.
  • Overload the draw method. There's no need for it to be generic. Let the compiler help you by choosing the right method for the job.
夏见 2024-07-26 01:52:49

定义一个抽象 Stencil 并让子类构造函数决定使用哪个模板类。

private Stencil s;

public void draw(){
   privateDraw(s)
}

private abstract void privateDraw(Stencil s);

Define an abstact Stencil and let the subclass constructor decide which stencil class to use.

private Stencil s;

public void draw(){
   privateDraw(s)
}

private abstract void privateDraw(Stencil s);
幸福%小乖 2024-07-26 01:52:48
public abstract class Shape<S extends Stencil>
{
   public abstract void draw( S stencil );
}

public class Square extends Shape<Stencil1>
{
   public void draw( Stencil1 stencil )
   {
     stencil.letsdo();
     stencil.some();
     stencil.drawing();
   }
}

public class Circle extends Shape<Stencil2>
{
   public void draw( Stencil2 stencil )
   {
      stencil.some();
      stencil.more();
      stencil.drawing();
   }
}
public abstract class Shape<S extends Stencil>
{
   public abstract void draw( S stencil );
}

public class Square extends Shape<Stencil1>
{
   public void draw( Stencil1 stencil )
   {
     stencil.letsdo();
     stencil.some();
     stencil.drawing();
   }
}

public class Circle extends Shape<Stencil2>
{
   public void draw( Stencil2 stencil )
   {
      stencil.some();
      stencil.more();
      stencil.drawing();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文