简体   繁体   English

是否可以在重写方法中使用专门的参数?

[英]Is it possible to have specialized parameters in overridden methods?

Let's say I have an abstract parent class called "shape", and that there are multiple subclasses (triangle, square, circle... ). 假设我有一个名为“shape”的抽象父类,并且有多个子类(三角形,正方形,圆形......)。 I want to define an abstract method in the parent "shape" class which all subclasses must implement, let's call it "draw". 我想在父“shape”类中定义一个抽象方法,所有子类都必须实现,我们称之为“draw”。 So all shape subclasses must provide the "draw()" method. 因此所有形状子类都必须提供“draw()”方法。 But, the draw method takes a parameter of type "Stencil", and, not every shape subclass can use just any stencil... 但是,draw方法采用“Stencil”类型的参数,并不是每个形状子类都可以使用任何模板......

So there is one abstract "shape" class, multiple shape subclasses, and multiple stencils. 因此,有一个抽象的“形状”类,多个形状子类和多个模板。 I need a draw method defined in the shape class. 我需要在shape类中定义一个draw方法。 A square might use Stencil1 and the circle might use Stencil2. 正方形可能使用Stencil1,圆圈可能使用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. 每个形状子类都需要使用特定的模板定义draw方法,因为这些类也被其他类使用,编译器应强制所有程序员使用该类支持的模板调用draw方法。 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" 我们不能定义像“public abstract void draw(Stencil s)”这样的抽象方法,因为那时程序员可以将任何模板传递给square类,而square类只支持“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. Update1:应该补充一下,shape类不关心子类使用哪个模板,但由于子类也在其他类中使用,因此定义draw方法以便编译器只接受支持的模板是很重要的。 。

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();
   }
}

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). 当然,你可以通过使用instanceof等解决这个问题。但是,这将导致一个非常令人困惑的API(如果你正在使用它)。

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. 因此,如果您将拥有多个“方形”模板,请创建一个SquareStencil抽象类型并从中派生具体实例。 You can even create them as a subset of the abstract Stencils class. 您甚至可以将它们创建为抽象Stencils类的子集。
  • 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. 让编译器通过为作业选择正确的方法来帮助您。

Define an abstact Stencil and let the subclass constructor decide which stencil class to use. 定义一个abstact Stencil,让子类构造函数决定使用哪个模板类。

private Stencil s;

public void draw(){
   privateDraw(s)
}

private abstract void privateDraw(Stencil s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM