java - 抽象类的目的/用途是什么? (寻找真实世界的例子。)

标签 java inheritance oop abstract-class

谁能告诉我 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/

相关文章:

swift - 从常规方法调用协议(protocol)默认实现

java - Eclipse 代码格式对于一个项目表现不同

java - 在android上解码加密信息

java - 将 java 转换为 actionscript 以使用 flash builder 有多容易?

c# - 我可以使用反射更改 C# 中的私有(private)只读继承字段吗?

visual-studio-2008 - Visual Studio - 自动实现从接口(interface)继承的所有方法

c# - oop:具体案例中的组合或继承

java - 将类 org.jruby.RubyArray 的实例转换为类 java.util.ArrayList

c++ - 使用基类中的函数静态调用纯虚函数的派生类实现

c# - 具有五个数字级别的分层大纲 - 如何插入兄弟或子行并调整现有记录?