Java - 抽象类转化为普通类

发布于 2024-10-17 03:50:58 字数 752 浏览 3 评论 0原文

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 个类扩展,然后就有了主类。我也必须检查并修改这些吗?

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

In this class, it handles the figure for creating a tree. I am trying to turn it into a normal class by simply giving a body to the abstract method. I noticed that when I remove the abstract tags, it still works perfectly normal. But my question is, if I want to make the class non-abstract, through what means would I go through to do this?

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?

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

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

发布评论

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

评论(7

洋洋洒洒 2024-10-24 03:50:58

你不应该改变图;你应该延长它。

该类由另外 2 个类扩展
类,然后它有主要的
班级。我必须经历并且
也修改那些吗?

更有理由不改变图:你会破坏代码的其余部分。

您不应该修改任何内容。创建一个扩展Figure 的新类,并使用您想要的行为重写抽象drawHere() 方法。

You shouldn't be altering Figure; you should be extending it.

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?

All the more reason to not alter Figure: you'll break the rest of the code.

You should not be modifying anything. Create a new class that extends Figure and override the abstract drawHere() method with the behavior you want.

余厌 2024-10-24 03:50:58

当您有一个抽象类时,

abstract class AbstractCar {

    float maxSpeed;
    Driver whoIsDriving;

    AbstractCar(float ms, Driver d) {
        maxSpeed = ms;
        if(!validateDriver(d)) throw new InvalidDriverException();
        whoIsDriving = d;
    }

    abstract boolean validateDriver(Driver d);

}

您可以通过扩展和定义相关的抽象方法来单独定义各种条件的行为。

class CrappyCar extends AbstractCar {    
    boolean validateDriver(Driver d) {
        return d.hasLicense() && d.hasInsurance();
    }
}

class ExpensiveCar extends AbstractCar {
    boolean validateDriver(Driver d) {
        return d.hasLicense() && d.hasInsurance() && d.hasGoodJobInCaseHeMessesUpMyCar();
    }
}

When you have an abstract class,

abstract class AbstractCar {

    float maxSpeed;
    Driver whoIsDriving;

    AbstractCar(float ms, Driver d) {
        maxSpeed = ms;
        if(!validateDriver(d)) throw new InvalidDriverException();
        whoIsDriving = d;
    }

    abstract boolean validateDriver(Driver d);

}

You can define the behavior separately for various conditions by extending and defining the abstract methods in question.

class CrappyCar extends AbstractCar {    
    boolean validateDriver(Driver d) {
        return d.hasLicense() && d.hasInsurance();
    }
}

class ExpensiveCar extends AbstractCar {
    boolean validateDriver(Driver d) {
        return d.hasLicense() && d.hasInsurance() && d.hasGoodJobInCaseHeMessesUpMyCar();
    }
}
蓝戈者 2024-10-24 03:50:58

如果您想要一个非抽象类,那么您不能使用 abstract 修饰符声明它(即只是 public class Figure)。不需要修改任何派生类(只要它们本身不是抽象的)。

If you want a non-abstract class, then you must not declare it with the abstract modifier (i.e. just public class Figure). There shouldn't be any need to modify any derived classes (so long as they themselves are not abstract).

凡尘雨 2024-10-24 03:50:58

从技术上讲,为了使抽象类成为非抽象类,您必须:

  • 为所有抽象方法提供实现
  • 由于您现在已经定义了所有内容的有效实现,因此删除所有抽象标记

无需修改继承类中的任何内容(假设它们本身是非抽象的),因为它们已经提供了其父级和父级的所有抽象方法的实现。可以自由地重写他们想要的任何方法。

是否应该让你的课程变得非抽象是另一个讨论点。

Technically speaking, in order to make an abstract class non-abstract you have to:

  • Provide implementation for all abstract methods
  • Since you now have a valid implementation of everything define, remove all abstract tags

There is no need to modify anything in inheriting classes (assuming they are non-abstract theirselves) because they already provide an implementation of all the abstract methods of their parent & are free to override any method they wish.

Whether or not you should make your class non-abstract is another point of discussion.

心凉 2024-10-24 03:50:58

您是正确的,删除​​ abstract 关键字并实现抽象方法会使类成为非抽象类。

但是,您通常不希望将类本身从抽象变为非抽象。在添加此关键字之前,类不是抽象的,因此您(或其他人)显然有理由确保它不是普通的类,而是抽象的类。

如果您从非常高的层次(远离 Java)考​​虑它,那么“树”就是您知道如何绘制的东西。同样,您可以想象一个子类“Circle”,您知道它的绘图应该是什么样子。然而,对于非常通用的“图形”,您不知道绘制它意味着什么。

这就是为什么实际绘图在 Figure 类中保留 abstract 的原因。因此,您不应该使 Figure 成为非抽象的,而应该关注从它扩展的类,并通过实现 Figure 中的所有抽象方法来使这些类成为非抽象的。在您的 Tree 类中,您知道 drawHere 应该做什么,因此在那里实现它来打印一棵树。在另一个类中,例如Circle,您可以以不同的方式实现它,但在Figure中实现它从来没有真正意义,因为您不知道要绘制什么。

You are correct that removing the abstract keywords and implementing the abstract methods makes the class non-abstract.

However, you normally do not want to turn the class itself from abstract to non-abstract. A class is not abstract until you add this keyword, so you (or someone else) apparently had a reason to make sure it's not a normal class, but an abstract one.

If you think about it at a very high level (far from Java), then a "Tree" is something you know how to draw. Similarly, you could imagine a subclass "Circle" for which you know what a drawing shoud look like. For the very generic "Figure", however, you have no idea what it means to draw it.

This is the idea of why the actual drawing is left abstract in your Figure class. Hence, you should not make Figure non-abstract, but instead focus on the classes that extend from it and make those non-abstract, by implementing all abstract methods from Figure. In your Tree class, you know what drawHere should do, so implement it there to print a tree. In another class, like Circle you implement it differently, but it never really makes sense to implement it in Figure, where you have no idea what to draw.

北陌 2024-10-24 03:50:58

您可以为 drawHere() 声明一个方法体(大概是空的,因为正如 @Frank 指出的那样,您实际上不知道如何绘制 Figure )并删除 abstract 修饰符。然后你将有一个具体的课程。这意味着有人可以创建一个新的Figure()。这不会是您现在拥有的两个子类中的任何一个,而只是一个 Figure

如果这样的对象(在调用drawHere()时不执行任何操作)没有用处(特别是,如果您认为拥有这样的对象是一个错误),那么你应该保持类抽象。即使您可以为每个方法定义一个实现,这种推理也适用。

You can declare a method body for drawHere() (presumably empty, since, as @Frank pointed out, you can't really have any idea about how to draw a Figure) and remove the abstract modifiers. Then you will have a concrete class. That means someone could create a new Figure(). This won't be either of the two subclasses you now have, just a Figure.

If such an object (that does nothing when it is called upon to drawHere()) would not be useful (and, in particular, if you would consider it an error to have such an object), then you should keep the class abstract. This reasoning applies even when you can define an implementation for every method.

大姐,你呐 2024-10-24 03:50:58

如果一个类是抽象的,您可以为所有方法提供主体,也可以不为这些方法提供主体,但如果任何类扩展抽象类,则它必须实现仅声明的所有方法。

If a class is made abstract you can give body to all the methodes or none of these but if any class is extending abstract class it must implement all the methode which is only being declared.

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