Cur*_*ico 4 java import enums resolve
我有2个分类在不同的页面.
对象类:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
主要课程:
import Sensor.Type;
public class MainClass {
public static void main(String[] args)
{
Sensor sun=new Sensor(Type.SONAR);
sun.TellIt();
}
Run Code Online (Sandbox Code Playgroud)
错误是两个,一个是Type无法解析,另一个是不能导入.我能做什么?我第一次使用枚举,但你看到了.
Rei*_*eus 10
enums需要在包中import声明语句才能工作,即enums无法从package-private(默认包)类中导入类.将枚举移动到包中
import static my.package.Sensor.Type;
...
Sensor sun = new Sensor(Type.SONAR);
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用完全限定的 enum
Sensor sun = new Sensor(Sensor.Type.SONAR);
Run Code Online (Sandbox Code Playgroud)
没有import语句