我想知道为什么在 Java 中我可以这样做
interface Specification{
double Interest =12.5;
void deposit(double);
}
class Base{
private String Account;
public Base(String acct){Account=acct;}
public void Show(){System.out.print("Account # "+Account);}
}
class Child extends Base implements Specification{
public Child (String acct){super(acct);Show();}
public void deposit(double cash){System.out.print("Now you have "+cash+" Dollars");}
}
但我做不到
class Child implements Specification extends Base{
public Child (String acct){super(acct);Show();}
public void deposit(double cash){System.out.print("Now you have "+cash+" Dollars");}
}
在同一 类 中使用 extends
和 implements
时,Java 中是否有特定的顺序或规则?
我希望有人能向我解释原因。
最佳答案
Because the Java Language Specification says so .一个普通的类声明遵循这种语法
NormalClassDeclaration:
ClassModifiers(opt) class Identifier TypeParameters(opt)
Super(opt) Interfaces(opt) ClassBody
Super
在哪里
Super:
extends ClassType
和接口(interface)
是
Interfaces:
implements InterfaceTypeList
在我看来,这是有道理的。您想先定义某物是什么,然后再定义它能做什么。
关于java - 实现和扩展一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22427085/