简体   繁体   English

我何时真正需要使用抽象方法?

[英]When do I really need to use Abstract Methods?

If I'm writing a program that draws shapes. 如果我正在编写一个绘制形状的程序。

abstract class GraphicObject {

    abstract void draw();
    abstract void resize();
}

and then a sub class that extends GraphicObject called Circle. 然后是扩展GraphicObject的子类,称为Circle。

class Circle : GraphicObject {
    void draw() {
    ...
    }
    void resize() {
        ...
    }
}

Would it make sense to handle it like this? 像这样处理它是否有意义? Surely some of the code for drawing is going to be repeated? 当然,一些绘图代码会重复吗?

So: 所以:

abstract class GraphicObject {

    public virtual void draw()
    {
       // all objects will need to be drawn in a similar way, do this here.
    }
}

and then in my circle class do all the unique stuff to the circle 然后在我的圈子课程中,为圈子做所有独特的事情

public override void draw() {
    //Do unique circle drawing stuff here
    base.draw();
}

then call the base draw method at the end of the method to finish. 然后在方法末尾调用基本draw方法以完成操作。

I struggle to see a scenario where I would need to really use an abstract method without repeating code in some form. 我很难看到需要真正使用抽象方法而无需以某种形式重复代码的情况。

Consider the case where the virtual method returns a value. 考虑虚拟方法返回值的情况。 Consider a factory style method which is used for creating values 考虑一种用于创建值的工厂样式方法

class Factory { 
  public abstract Node CreateNode(); 
}

An abstract method works well here because it doesn't put any responsibility on Factory to actually produce a Node value. 抽象方法在这里效果很好,因为它对Factory实际产生Node值不承担任何责任。 Instead it just declares a Node should be provided. 相反,它只是声明应提供一个Node If instead you made it a virtual method then Factory would have to pick a default value. 如果改为将其设置为虚拟方法,则Factory必须选择默认值。 How can it produce a Node if it doesn't know what type of factory it is? 如果不知道工厂是什么类型,如何生产Node

public virtual Node CreateNode() { 
  return ???;
}

Unless you have things that must be done for all shapes at the beginning or ending of the draw call, then you wouldn't put code in the abstract class to do it. 除非在绘图调用的开始或结束时必须对所有形状进行处理,否则您不会将代码放在抽象类中进行处理。

If there are common things to be done that are done at different points during draw, then you can put them in the abstract class in, say, protected methods; 如果在绘制过程中有不同的事情要做,可以将它们放在抽象类中,例如,受保护的方法中。 only classes that inherit will be able to call them, and that will put that common code into one place. 只有继承的类才可以调用它们,并将这些通用代码放在一个地方。

The use of abstract methods is to ensure that (1) a class using a shape has access to those methods without knowing what subclass it holds a reference to. 抽象方法的使用是为了确保(1)使用形状的类可以访问这些方法,而无需知道它持有对哪个子类的引用。 So, for instance, a page could have a list of shapes, and call draw on each one (because it is defined in the superclass) even though each one is a specific subclass shape. 因此,例如,一个页面可能具有一系列形状,并在每个形状上调用draw(因为它是在超类中定义的),即使每个形状都是特定的子类形状。 If draw() is not defined in shape, then you cannot call draw on each shape in a list. 如果未在shape中定义draw(),则无法在列表中的每个形状上调用draw。

(I am more familiar with java and have used java terminology -- I gather you are more familiar with something else, given the format for your inheritance and the use of "base.". Translate as necessary...) (我更熟悉Java并使用过Java术语-鉴于您的继承格式和使用“ base”,我认为您对其他内容更加熟悉。根据需要进行翻译...)

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

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