Java - 抽象类转化为普通类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你不应该改变图;你应该延长它。
更有理由不改变图:你会破坏代码的其余部分。
您不应该修改任何内容。创建一个扩展Figure 的新类,并使用您想要的行为重写抽象drawHere() 方法。
You shouldn't be altering Figure; you should be extending it.
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.
当您有一个抽象类时,
您可以通过扩展和定义相关的抽象方法来单独定义各种条件的行为。
When you have an abstract class,
You can define the behavior separately for various conditions by extending and defining the abstract methods in question.
如果您想要一个非抽象类,那么您不能使用
abstract
修饰符声明它(即只是public class Figure
)。不需要修改任何派生类(只要它们本身不是抽象的)。If you want a non-abstract class, then you must not declare it with the
abstract
modifier (i.e. justpublic class Figure
). There shouldn't be any need to modify any derived classes (so long as they themselves are not abstract).从技术上讲,为了使抽象类成为非抽象类,您必须:
无需修改继承类中的任何内容(假设它们本身是非抽象的),因为它们已经提供了其父级和父级的所有抽象方法的实现。可以自由地重写他们想要的任何方法。
是否应该让你的课程变得非抽象是另一个讨论点。
Technically speaking, in order to make an abstract class non-abstract you have to:
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.
您是正确的,删除
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 yourFigure
class. Hence, you should not makeFigure
non-abstract, but instead focus on the classes that extend from it and make those non-abstract, by implementing all abstract methods fromFigure
. In yourTree
class, you know whatdrawHere
should do, so implement it there to print a tree. In another class, likeCircle
you implement it differently, but it never really makes sense to implement it inFigure
, where you have no idea what to draw.您可以为
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 aFigure
) and remove theabstract
modifiers. Then you will have a concrete class. That means someone could create anew Figure()
. This won't be either of the two subclasses you now have, just aFigure
.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.如果一个类是抽象的,您可以为所有方法提供主体,也可以不为这些方法提供主体,但如果任何类扩展抽象类,则它必须实现仅声明的所有方法。
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.