[英]Java - Issue with functions/varibles inside an enum and a constructor
I have been trying to understand what is going on in this piece of Java code, but so far I didn't manage. 我一直在试图理解这段Java代码中发生了什么,但是到目前为止我还没有管理。 This is part of this library .
这是该库的一部分。
My wonders are on 我的奇迹在
CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(Constants.VAR_NET_BOUNDING_BOX)
They do not appear previously in the code and later on, there is one constructor which uses CURRENT_SIM_TIME
without any brackets ()
. 它们以前没有出现在代码中,后来又没有出现,因此有一个构造函数使用
CURRENT_SIM_TIME
而不带任何方括号()
。
enum Variable {
CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(
Constants.VAR_NET_BOUNDING_BOX), ;
public final int id;
private Variable(int id) {
this.id = id;
}
}
This the constructor I was talking about: 我正在谈论的这个构造函数:
SimulationData(DataInputStream dis, DataOutputStream dos) {
super("", Variable.class);
addReadQuery(Variable.CURRENT_SIM_TIME,
new ReadObjectVarQuery.IntegerQ(dis, dos,
Constants.CMD_GET_SIM_VARIABLE, "",
Variable.CURRENT_SIM_TIME.id) {
});
addReadQuery(Variable.NET_BOUNDARIES,
new ReadObjectVarQuery.BoundingBoxQ(dis, dos,
Constants.CMD_GET_SIM_VARIABLE, "",
Variable.NET_BOUNDARIES.id));
this.dis = dis;
this.dos = dos;
}
So, how is this possible? 那么,这怎么可能呢?
Inside the enum Variable we have CURRENT_SIM_TIME
and NET_BOUNDARIES
with an argument inside the brackets and then in the constructor we create an addReadQuery
with Variable.CURRENT_SIM_TIME
as well as Variable.NET_BOUNDARIES
, without brackets as an argument. 里面的枚举变量,我们有
CURRENT_SIM_TIME
和NET_BOUNDARIES
用括号内的参数,然后在构造函数中,我们创建一个addReadQuery
与Variable.CURRENT_SIM_TIME
以及Variable.NET_BOUNDARIES
,不带括号作为参数。
Are these functions? 这些功能吗? Static variables?
静态变量? Are they be defined in some other part of the code and I am not able to find them?
它们是否在代码的其他部分中定义,而我找不到它们?
I am really lost right now... 我现在真的迷路了...
Declaring Enum : 声明枚举:
enum Variable {
CURRENT_SIM_TIME(Constants.VAR_TIME_STEP), NET_BOUNDARIES(
Constants.VAR_NET_BOUNDING_BOX), ;
public final int id;
private Variable(int id) {
this.id = id;
}
}
So when you say CURRENT_SIM_TIME(Constants.VAR_TIME_STEP)
inside your enum declaration, you are calling the constructor Variable(int id)
such that for enum CURRENT_SIM_TIME
the value of the id
variable will be set to Constants.VAR_TIME_STEP
for that enum itself. 因此,当您在枚举声明中说
CURRENT_SIM_TIME(Constants.VAR_TIME_STEP)
,您在调用构造函数Variable(int id)
,以便对于枚举CURRENT_SIM_TIME
,该枚举本身的id
变量的值将设置为Constants.VAR_TIME_STEP
。
Similar is the case for NET_BOUNDARIES
NET_BOUNDARIES
也是如此
So if you want to get the value of id
for CURRENT_SIM_TIME
, you would do it like : 因此,如果要获取
CURRENT_SIM_TIME
的id
的值,则可以这样做:
Variable.NET_BOUNDARIES.id
Using Enum 使用枚举
And when you use enum, you don't have to call it with constructor, because that's already done when you declare your enum. 使用枚举时,无需使用构造函数调用它,因为在声明枚举时已经完成了。 So you just use the enum.
因此,您只需要使用枚举即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.