【问题标题】:implements and extends a class实现和扩展一个类
【发布时间】:2014-04-21 01:09:01
【问题描述】:

我想知道为什么在 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");}
    }

在同一个中使用extendsimplements 时,Java 中是否有任何特定的顺序或规则。 我想请人解释一下原因。

【问题讨论】:

    标签: java using extends implements


    【解决方案1】:

    Because the Java Language Specification says so。普通的类声明遵循这种语法

    NormalClassDeclaration:
        ClassModifiers(opt) class Identifier TypeParameters(opt)
                                                   Super(opt) Interfaces(opt) ClassBody
    

    Super 在哪里

    Super:
        extends ClassType
    

    Interfaces

    Interfaces:
        implements InterfaceTypeList
    

    在我看来,这是有道理的。你想在定义它可以做什么之前定义它是什么。

    【讨论】:

      【解决方案2】:

      是的,在 java 中有特定的 order first class in extended 然后你可以实现很多类:

      class Child extends Base implements Specification1,Specification2.. -> valid 
      

      &

      class Child implements Specification1,Specification2.. extends Base -> not valid 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-02
        • 1970-01-01
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2010-10-13
        • 1970-01-01
        相关资源
        最近更新 更多