枚举无法解决? Java的 [英] Enum can not be resolved? Java

查看:193
本文介绍了枚举无法解决? Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对象类:

  public class Sensor {

类型类型;
public static枚举类型
{
PROX,SONAR,INF,CAMERA,TEMP;
}

public传感器(类型类型)
{
this.type = type;
}

public void TellIt()
{
switch(type)
{
case PROX:
System.out .println(传感器类型为Proximity);
break;
case SONAR:
System.out.println(传感器的类型是声纳);
break;
case INF:
System.out.println(传感器类型为红外线);
break;
case CAMERA:
System.out.println(传感器的类型是相机);
break;
case TEMP:
System.out.println(传感器类型为温度);
break;
}
}

public static void main(String [] args)
{
传感器sun =新传感器(Type.CAMERA);
sun.TellIt();
}
}

主类:

  import Sensor.Type; 

public class MainClass {

public static void main(String [] args)
{
传感器sun =新的传感器(Type.SONAR);
sun.TellIt();
}

错误是两个,一个是类型无法解决其他是不能导入。我能做什么?我第一次使用枚举,但是你看到。

解决方案

枚举要在 import 语句的包中声明,例如从package-private(默认包)中的类导入枚举 )类是不可能的。将枚举移动到包

  import static my.package.Sensor.Type; 
...
传感器sun =新传感器(Type.SONAR);

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

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

没有import语句


I have 2 classed at different pages.

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.

解决方案

enums are required to be declared in a package for import statements to work, i.e. importing enums from classes in package-private (default package) classes is not possible. Move the enum to a package

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

Alternatively you can use the fully qualified enum

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

without the import statement

这篇关于枚举无法解决? Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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