java 枚举不能解决?爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17858158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 17:49:33  来源:igfitidea点击:

Enum can not be resolved? Java

javaimportenumsresolve

提问by CursedChico

I have 2 classed at different pages.

我有 2 个分类在不同的页面。

The object class:

对象类:

public class Sensor {

  Type type;
  public static enum Type
  {
        PROX,SONAR,INF,CAMERA,TEMP;
  }

  public Sensor(Type type)
  {
  this.type=type;
  }

  public void TellIt()
  {
      switch(type)
      {
      case PROX: 
          System.out.println("The type of sensor is Proximity");
          break;
      case SONAR: 
          System.out.println("The type of sensor is Sonar");
          break;
      case INF: 
          System.out.println("The type of sensor is Infrared");
          break;
      case CAMERA: 
          System.out.println("The type of sensor is Camera");
          break;
      case TEMP: 
          System.out.println("The type of sensor is Temperature");
          break;
      }
  }

  public static void main(String[] args)
    {
        Sensor sun=new Sensor(Type.CAMERA);
        sun.TellIt();
    }
    }

Main class:

主类:

import Sensor.Type;

public class MainClass {

public static void main(String[] args)
{
    Sensor sun=new Sensor(Type.SONAR);
    sun.TellIt();
}

Errors are two, one is Type can not be resolved other is cant not import. What can i do? I first time used enums but you see.

错误有两种,一种是类型无法解析,另一种是无法导入。我能做什么?我第一次使用枚举,但你看。

回答by Reimeus

enumsare required to be declared in a package for importstatements to work, i.e. importing enumsfrom classes in package-private (default package) classes is not possible. Move the enum to a package

enums需要在包中import声明才能使语句起作用,即enums无法从包私有(默认包)类中的类导入。将枚举移动到包

import static my.package.Sensor.Type;
...
Sensor sun = new Sensor(Type.SONAR);

Alternatively you can use the fully qualified enum

或者,您可以使用完全限定的 enum

Sensor sun = new Sensor(Sensor.Type.SONAR);

without the import statement

没有导入语句

回答by Veera

For static way give proper package structure in the static import statement

对于静态方式,在静态导入语句中给出适当的包结构

import static org.test.util.Sensor.Type;
import org.test.util.Sensor;
public class MainClass {
    public static void main(String[] args) {
        Sensor sun = new Sensor(Type.SONAR);
        sun.TellIt();
    }
}

回答by ThePoltergeist

The static keyword has no effect on enum. Either use the outer class reference or create the enum in its own file.

static 关键字对枚举没有影响。使用外部类引用或在其自己的文件中创建枚举。