【问题标题】:semicolon/curly bracket syntax error in javajava中的分号/大括号语法错误
【发布时间】:2016-06-06 06:21:55
【问题描述】:

当我尝试下面的代码时,我得到了以下异常:

标记“;”的语法错误,{ 预期的

public int getValue(int row, int column) ;
    return row + column;

但是当我把它改成下面的代码时,我得到了这个异常:

抽象方法不指定主体

public int getValue(int row, int column) {
    return row + column;
}

如何在不出现语法错误的情况下编写此代码?

【问题讨论】:

标签: java methods syntax interface


【解决方案1】:

我做了一个有效的例子,我希望它有所帮助。如果您有任何问题,大多数评论都会尽力提供帮助。

public class Test {
    private int array[][] = new int[10][10];
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.getValue(1, 5));
        System.out.println(test.getValue2(2, 3));   
    }

    //Get the value of parameters
    public int getValue(int row, int column){
        return (row+column);
    }
    //Get the value of an attribute
    public int getValue2(int row, int column){
        return array[row][column];
    }

}

如果你想在一个界面上做一个声明,它看起来像这样:

public interface TestInterface {
    default int getValue(int row, int column){
        return row + column;
    }
}

如果这是目标,那么你有一些架构问题,但我认为这只是名称“接口”而不是“类”。

【讨论】:

    【解决方案2】:

    我认为这是一个接口类,这就是为什么您会收到“抽象方法未指定主体”错误。请通过这个链接,它可以让你更好地了解interface in java.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 2013-08-24
      • 2014-09-30
      • 2012-02-29
      • 2021-05-06
      相关资源
      最近更新 更多