Java - 抽象类到普通类

标签 java class abstract

public abstract class Figure
{
  private int offset;

  public Figure()
  {
    offset = 0;
  }

  public Figure(int theOffset)
  {
    offset = theOffset;
  }

  public void setOffset(int newOffset)
  {
    offset = newOffset;
  }

  public int getOffset()
  {
    return offset;
  }

  public abstract void drawHere();

  /**
   * Draws the figure at lineNumber lines down from the
   * current line.
   */

  public void drawAt(int lineNumber)
  {
    int count;
    for(count = 0; count < lineNumber; count++)
      System.out.println();
    drawHere();
  }
}

在这个类中,它处理用于创建树的图形。我试图通过简单地给抽象方法一个主体来把它变成一个普通的类。我注意到当我删除抽象标签时,它仍然可以正常工作。但我的问题是,如果我想让这个类成为非抽象类,我会通过什么方式来做到这一点?

这个类由其他 2 个类扩展,然后它有主类。我是否也必须检查并修改它们?

最佳答案

你不应该改变 Figure;你应该扩展它。

This class is extended upon by 2 other classes and then it has the main class. Do I have to go through and modify those too?

更有理由不更改 Figure:您将破坏其余代码。

你不应该修改任何东西。创建一个扩展 Figure 的新类,并用您想要的行为覆盖抽象的 drawHere() 方法。

关于Java - 抽象类到普通类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987514/

相关文章:

C++/Qt : passing variables to be changed in the class

java - 在我的应用程序上生成 APK 构造函数问题

java - 是否有可能有一个具有抽象和完整方法的 Java 接口(interface)?

flutter - 你如何在 dart 中使用抽象类进行继承?错误 : superclass SpanishData doesn't have a zero argument constructor

java - Spring 问题 - URI 的数量必须为偶数

java - 将 ZipEntry 复制到新 ZipFile 的惯用方法是什么?

java - 将数据库字段值加载到 JCombobox

Java 1.7 com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException : Cannot resolve element with ID data

java - 从类和 javadoc 生成代码 stub

C#抽象类有另一个抽象类对象