Java错误:< identifier>预期在受保护的接口内部 [英] Java Error: <identifier> expected inside protected interface

查看:48
本文介绍了Java错误:< identifier>预期在受保护的接口内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临以下问题.当我遇到问题时,我试图编写一个有关Java策略的简单教程.编译器在界面上的 void do(int i); 上给我一个< identifier>预期"错误:这是完整的课程:

I am facing the following problem. I was trying to put together a simple tutorial on the strategy in Java when I ran into a problem. The compiler gives me an "<identifier> expected" error on the void do(int i); in the interface: Here is the full class:

import java.util.*;
public class Data {
private List<Integer> ints;

  public Data( int[] a ) {
    ints = new LinkedList<>(); 
    for( int i : a ) ints.add( i );
 }

 protected static interface Strategy{
    void do(int i);
 }

 protected void loop( Strategy s ) {
        for( int i : ints ) {
            s.do( i );
        }
    }
 }

为什么会出现此错误?提前非常感谢您.

Why am I getting this error? Thank you very much in advance.

推荐答案

方法名称 do

The method name do is a Java keyword (Section 3.9, JLS) and can't be a method name. Change your method name to something not a keyword.

protected static interface Strategy{
    void doAction(int i);
}

这篇关于Java错误:&lt; identifier&gt;预期在受保护的接口内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆