Such as from the Java docs:
比如来自Java文档:
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
In Ruby, I believe these would be Symbols
. From the Ruby-Doc: "Symbol
objects represent names and some Strings
inside the Ruby interpreter". What are these in Java and how does the compiler treat them? As a Token
or a Reference Literal?
在Ruby中,我认为这些都是符号。在Ruby文档中:“符号对象表示Ruby解释器中的名称和一些字符串”。这些在Java中是什么?编译器如何处理它们?作为标记还是引用文字?
3 个解决方案
#1
2
They are constant instances of the enum
class, check out the javadoc and the actual implementation if you are interested. So if you have this:
它们是enum类的常量实例,如果您感兴趣,请查看javadoc和实际实现。所以如果你有这个:
public enum Weekday
{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
it will internally be translated to:
它将在内部被翻译成:
class Weekday extends Enum
{
public static final Weekday MONDAY = new Weekday( "MONDAY", 0 );
public static final Weekday TUESDAY = new Weekday( "TUESDAY ", 1 );
// more constants
private Weekday( String s, int i )
{
super( s, i );
}
// more methods
}
(Example from here, sorry it is not an english resource)
(举例来说,对不起,这不是英语资源)
#2
2
MONDAY
is an instance of Enum<Day>
, a static final field of the class Day
. Each Enum<Day> contains a single field 'String name'. You can do:
周一是Enum
Enum<Day> x = Day.MONDAY;
Instances of enum
s can be used in switch
statements, they are a typed replacement for strings in some cases.
在switch语句中可以使用enums实例,在某些情况下它们是字符串的类型替换。
Enums are also often used to thread-safely create singletons:
枚举也经常用于线程安全的创建单例:
enum MySingleton{
INSTANCE;
void instanceMethod();
}
#3
1
enum in java are objects of class enum
as described here and they are complete objects in java
java中的enum是这里描述的类枚举的对象,它们是java中的完整对象。
meaning they can have instance and class methods
这意味着它们可以有实例和类方法。
you can find in here,here and here that is a generic object
... can be predefined or of type Object
你可以在这里找到,这里和这里这是一个通用对象…可以预定义或类型对象?
you can read more about Generics
in java to get familiar with it
您可以在java中了解更多关于泛型的知识,以便熟悉它。
#1
2
They are constant instances of the enum
class, check out the javadoc and the actual implementation if you are interested. So if you have this:
它们是enum类的常量实例,如果您感兴趣,请查看javadoc和实际实现。所以如果你有这个:
public enum Weekday
{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
it will internally be translated to:
它将在内部被翻译成:
class Weekday extends Enum
{
public static final Weekday MONDAY = new Weekday( "MONDAY", 0 );
public static final Weekday TUESDAY = new Weekday( "TUESDAY ", 1 );
// more constants
private Weekday( String s, int i )
{
super( s, i );
}
// more methods
}
(Example from here, sorry it is not an english resource)
(举例来说,对不起,这不是英语资源)
#2
2
MONDAY
is an instance of Enum<Day>
, a static final field of the class Day
. Each Enum<Day> contains a single field 'String name'. You can do:
周一是Enum
Enum<Day> x = Day.MONDAY;
Instances of enum
s can be used in switch
statements, they are a typed replacement for strings in some cases.
在switch语句中可以使用enums实例,在某些情况下它们是字符串的类型替换。
Enums are also often used to thread-safely create singletons:
枚举也经常用于线程安全的创建单例:
enum MySingleton{
INSTANCE;
void instanceMethod();
}
#3
1
enum in java are objects of class enum
as described here and they are complete objects in java
java中的enum是这里描述的类枚举的对象,它们是java中的完整对象。
meaning they can have instance and class methods
这意味着它们可以有实例和类方法。
you can find in here,here and here that is a generic object
... can be predefined or of type Object
你可以在这里找到,这里和这里这是一个通用对象…可以预定义或类型对象?
you can read more about Generics
in java to get familiar with it
您可以在java中了解更多关于泛型的知识,以便熟悉它。