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/