以下哪一项 class 定义是无法实例化的 class 的有效定义? (摘要 classes)

Which one of the following class definitions is a valid definition of a class that cannot be instantiated? (Abstract classes)

我不明白为什么答案不是 B 和 C 而只是 C。

一个。无效,因为你需要使 class 抽象,因为里面有一个抽象函数。

class Ghost
{
    abstract void haunt();
}

乙。这不是正确的答案,但我不明白为什么,因为它是一个抽象 class,并且它们不能被实例化。另一个选项是它是一个无效的定义,但我不明白它怎么会无效,它只有一个函数调用。

abstract class Ghost
{
    void haunt();
}

C。这是正确答案

abstract class Ghost
{
    void haunt() { };
}

D.它不是 class 而是一种方法,因此无法实例化

abstract Ghost
{
    abstract void haunt();
}

E.需要 'abstract' 而不是 'static'

static class Ghost
{
    abstract haunt();
}

选项B

void haunt();

不是方法调用,它实际上是一个方法定义,它是无效的,因为它没有方法体,也没有标记为抽象。

B 无效,因为方法 haunt 没有实现,也没有声明 abstract。应该是

abstract class Ghost{
   abstract void haunt();
}

只有接口可以在不显式声明抽象方法的情况下声明抽象方法

interface Gost{
  void haunt(); 
}