-2

由于 Eclipse IDE“这么说”,我在这两个小错误中遇到了一些困难。我会指出错误。老实说,我不知道如何深入解释这些错误。我认为它们很简单,我无法让错误消失。

    ScheduledExecutorService timer = Executors.newScheduledThreadPool (1);
    timer.scheduleAtFixedRate(new Runnable() 
    {
        public void run() 
        {
            if (lists. size ()> 0) 
            {
                boolean lighted = Lamp.valueOf(Road.this.name).isLighted(); //According to Eclipse, "The method valueOf(Class<T>, String) in the type Enum<Lamp> is not applicable for the arguments (String)"

                if (lighted) 
                {
                    System.out.println(lists.remove(0) + "is traversing!");
                }
            }
        }
    }, 1,1, TimeUnit. SECONDS);

和我的不同班级我的包中的另一个错误

public Lamp Blackout() 
{
    this.lighted = false;

    if (opposite != null) 
    {
        Lamp.valueOf(opposite).in.Blackout(); //in cannot be resolved or is not a field. It suggests me to create enum constant, which I did and it wouldn't work either. 
    }

    Lamp nextLamp = null;

    if (next != null) 
    {
        nextLamp = Lamp.valueOf(next);
        System.out.println("Green" + name () + "--------> switch to" + next);
        nextLamp.light();
    }
    return nextLamp;
}
4

2 回答 2

4

你的第一个错误 Lamp.valueOf(Road.this.name).isLighted();

//根据Eclipse,“Enum类型中的方法valueOf(Class, String)不适用于参数(String)”

Lamp.valueOf() 方法需要两个参数,首先是一个类参数,然后是一个字符串参数。您刚刚在方法中传递了一个 String 参数,这就是 eclipse 抛出错误的原因。

你第二个错误

Lamp.valueOf(opposite).in.Blackout();

//in 无法解析或不是字段。

在我看来,它的语法不正确。彻底检查您的代码。在您的代码方法中被链接。in不应该在那里。或者它可能是一种方法in()

于 2015-05-02T20:07:09.700 回答
2

在黑暗中拍摄,因为您没有公开所有相关代码,但您可以尝试在valueOf此处添加缺少的参数:

boolean lighted = Lamp.valueOf(Lamp.class, Road.this.name).isLighted();

并在这里调用该in() 方法

Lamp.valueOf(Lamp.class, opposite).in(Blackout());

请遵循Java 代码风格约定;方法名称应以小写字母开头,因此 blackout 方法签名应如下所示:

public Lamp blackout()

如果没有看到 的代码Lamp enum,就不可能知道后一种情况的确切问题是什么。

于 2015-05-02T20:10:07.090 回答