java 如何覆盖子类中的抽象方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39152956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to override an abstract method in a subclass?
提问by Justin Middleton
I have to create subclasses for an abstract parent class which print out shapes, however whenever I try to create an object it keeps telling me that I cannot instantiate an abstract class, and when I remove the keyword abstract
from my code that overrides, it says I can't do that either.
我必须为打印出形状的抽象父类创建子类,但是每当我尝试创建一个对象时,它总是告诉我我无法实例化抽象类,并且当我abstract
从我的代码中删除覆盖的关键字时,它说我也不能那样做。
My code:
我的代码:
public class Rectangle extends VectorObject {
protected int ID, x, y, xlnth, ylnth;
protected int matrix[][];
Rectangle(int id, int ax, int ay, int xlen, int ylen) {
super(id, ax, ay);
xlnth = xlen;
ylnth = ylen;
}
public int getId() {
return ID;
}
public void draw() {
String [][] matrix = new String[20][20];
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (i == x) {
matrix[i][y] = "*";
}
if (j==y) {
matrix[i][y] = "*";
}
System.out.println(matrix[i][y]);
}
}
}
}
Abstract parent class:
抽象父类:
abstract class VectorObject {
protected int id, x, y;
VectorObject(int anId, int ax, int ay) {
id = anId;
x = ax;
y = ay;
}
int getId() {
return id;
}
void setNewCoords(int newx, int newy) {
x = newx;
y = newy;
}
public abstract void draw (char [][] matrix);
}
回答by Orin
When you define an abstract method in a class, you are saying that subclasses of this object MUSTimplement them, unless they are another abstract class. So when you are extending the VectorObject
with the Rectange
class, you must implement a draw
method with the same parameters.
当您在类中定义抽象方法时,您是说该对象的子类必须实现它们,除非它们是另一个抽象类。所以当你VectorObject
用Rectange
类扩展时,你必须实现一个draw
具有相同参数的方法。
Looking at the header of the function you provided in VectorObject
:
查看您提供的函数的标题VectorObject
:
public abstract void draw ( char [][] matrix );
public abstract void draw ( char [][] matrix );
Now lets look at the header of the function provided in Rectange
:
现在让我们看一下中提供的函数的标题Rectange
:
public void draw()
public void draw()
These are not the same, so it is not considered an Override and there is an error, because you have not implemented the method draw( char[][] matrix )
这些不一样,所以不认为是 Override 并且有错误,因为你没有实现该方法 draw( char[][] matrix )
The correct way to implement in Rectange
would be:
正确的实现方法Rectange
是:
public class Rectangle extends VectorObject {
//... various methods and variable declarations.
@Override
public void draw( char[][] matrix ) {
//... draw the Rectangle object
}
}
When we add the @Override
annotation, we are telling the compiler that we are overriding a parent classes method. You should always use this annotation when implementing a parent class method, as it will let you know if you somehow messed up the signature, and it is easier to understand by other developers.
当我们添加@Override
注释时,我们告诉编译器我们正在覆盖父类方法。在实现父类方法时,您应该始终使用此注释,因为它会让您知道是否以某种方式弄乱了签名,并且其他开发人员更容易理解。
回答by GhostCat
In order to overridea method, they must have the matching signatures.
为了覆盖一个方法,它们必须具有匹配的签名。
The abstract parent method has char[][] matrix
; whereas the child method doesn't take any arguments. Thus you are notoverriding.
抽象父方法有char[][] matrix
; 而 child 方法不接受任何参数。因此,您不是压倒性的。
In other words: overridingis more than just having two methods with the same name!
换句话说:覆盖不仅仅是拥有两个同名的方法!
The easy thing that you should alwaysdo: put the @Override annotation on any method of which you think: I am overriding something here.
您应该始终做的一件简单的事情:将@Override 注释放在您认为的任何方法上:我在这里覆盖了某些东西。
If you do that know, the compiler will tell you immediately that the Rectangle version of draw() doesn't override anything!
如果你知道,编译器会立即告诉你 draw() 的 Rectangle 版本不会覆盖任何东西!
回答by Chetan Arora
Method signature is not the same. The parameters of the parent and child are different. Try adding @Override annotation to child class method and it will show you the problems @Override annotation ensure that all the mandatory conduct of overriding a method are followed. The method annotated would be checked against the parent method. If there are no method with matching signature it will show compilation errors.
方法签名不一样。parent 和 child 的参数是不同的。尝试在子类方法中添加@Override 注释,它会向您展示问题@Override 注释确保遵循覆盖方法的所有强制性行为。注释的方法将根据父方法进行检查。如果没有匹配签名的方法,它将显示编译错误。
In your case the parent abstract class method you need to override is public abstract void draw (char [][] matrix) whereas the child class method signature is public void draw() so the arguments mismatch
在您的情况下,您需要覆盖的父抽象类方法是 public abstract void draw (char [][] matrix) 而子类方法签名是 public void draw() 所以参数不匹配