谁能告诉我 Java 抽象类的例子?具有实际应用程序(而不是教科书示例)的东西会更可取。
谢谢!
最佳答案
在 Abstract Methods and Classes 上阅读本教程.
First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
关于java - 抽象类的目的/用途是什么? (寻找真实世界的例子。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3137730/