Java中的“抽象类”是什么?


当前回答

An abstract class is one that isn't fully implemented but provides something of a blueprint for subclasses. It may be partially implemented in that it contains fully-defined concrete methods, but it can also hold abstract methods. These are methods with a signature but no method body. Any subclass must define a body for each abstract method, otherwise it too must be declared abstract. Because abstract classes cannot be instantiated, they must be extended by at least one subclass in order to be utilized. Think of the abstract class as the generic class, and the subclasses are there to fill in the missing information.

其他回答

Java类在以下条件下变成抽象类:

1. 至少有一个方法被标记为abstract:

public abstract void myMethod()

在这种情况下,编译器会强制您将整个类标记为抽象类。

2. 该类被标记为abstract:

abstract class MyClass

如前所述:如果你有一个抽象方法,编译器会强迫你将整个类标记为抽象。但是即使你没有任何抽象方法,你仍然可以把类标记为抽象的。

常见的使用:

抽象类的一个常见用途是提供类的大纲,类似于接口。但与接口不同的是,它已经可以提供功能,即类的某些部分已经实现,而另一些部分只是用方法声明来概述。(“摘要”)

抽象类不能被实例化,但是可以基于抽象类创建具体类,然后可以实例化抽象类。要做到这一点,你必须继承抽象类并重写抽象方法,即实现它们。

An abstract class is one that isn't fully implemented but provides something of a blueprint for subclasses. It may be partially implemented in that it contains fully-defined concrete methods, but it can also hold abstract methods. These are methods with a signature but no method body. Any subclass must define a body for each abstract method, otherwise it too must be declared abstract. Because abstract classes cannot be instantiated, they must be extended by at least one subclass in order to be utilized. Think of the abstract class as the generic class, and the subclasses are there to fill in the missing information.

它是一个不能被实例化的类,并且强制实现类尽可能地实现它概述的抽象方法。

来自oracle文档

抽象方法和类:

抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法 抽象类不能被实例化,但可以被子类化

抽象方法是一种声明时没有实现(没有大括号,后面跟着一个分号)的方法,如下所示:

abstract void moveTo(double deltaX, double deltaY);

如果一个类包含抽象方法,那么类本身必须声明为抽象的,如下所示:

public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。然而,如果不是这样,那么子类也必须声明为抽象的。

由于抽象类和接口是相关的,看看下面的SE问题:

接口和抽象类的区别是什么?

我应该如何解释接口和抽象类之间的区别?

解决方案——基类(抽象)

public abstract class Place {

String Name;
String Postcode;
String County;
String Area;

Place () {

        }

public static Place make(String Incoming) {
        if (Incoming.length() < 61) return (null);

        String Name = (Incoming.substring(4,26)).trim();
        String County = (Incoming.substring(27,48)).trim();
        String Postcode = (Incoming.substring(48,61)).trim();
        String Area = (Incoming.substring(61)).trim();

        Place created;
        if (Name.equalsIgnoreCase(Area)) {
                created = new Area(Area,County,Postcode);
        } else {
                created = new District(Name,County,Postcode,Area);
        }
        return (created);
        }

public String getName() {
        return (Name);
        }

public String getPostcode() {
        return (Postcode);
        }

public String getCounty() {
        return (County);
        }

public abstract String getArea();

}