我正在将大量代码从PHP移植到Java(Android应用程序)。一切都已完成,但我在这里面临一个问题,
我有这个PHP代码:
class SenderKeyGroupData{
const MESSAGE = 1;
const SENDER_KEY = 2;
/* @var array Field descriptors */
protected static $fields = array(
self::MESSAGE => array(
'name' => 'message',
'required' => false,
'type' => 7,
),
self::SENDER_KEY => array(
'name' => 'sender_key',
'required' => false,
'type' => "SenderKeyGroupMessage",
),
);
但是我想用Java编写这个代码,例如我创建了这个Java代码:
class SenderKeyGroupData {
int MESSAGE = 1; // Must be const
int SENDER_KEY = 2; // Must be const
// These two Maps must be in the $fields array?
Map MESSAGE = new HashMap(); //Duplicate conflict here
MESSAGE.put("name", "message");
MESSAGE.put("required", false); // Map can't contain booleans?
MESSAGE.put("type", "7");
Map SENDER_KEY = new HashMap(); //Duplicate conflict here
SENDER_KEY.put("name", "sender_key");
SENDER_KEY.put("required", false); // Map can't contain booleans?
SENDER_KEY.put("type", "SenderKeyGroupMessage");
}
我将问题描述为评论。欢迎所有的想法。
请注意,const在构造函数中包含1和2作为值,但也会分配一个数组。所以请举一个例子,而不是将其指向重复。
答案 0 :(得分:0)
我真的不知道您需要如何以及在何处访问这些变量或者您打算如何使用此类但这至少可以编译:
class SenderKeyGroupData {
public static final int MESSAGE = 1; // final keyword means it cannot be changed
public static final int SENDER_KEY = 2; // public keyword means it can be accessed by anyone
static Map MESSAGE_MAP = new HashMap(); // static means this is shared between all instances
static {
MESSAGE_MAP.put("name", "message");
MESSAGE_MAP.put("required", false);
MESSAGE_MAP.put("type", "7");
}
static Map SENDER_KEY_MAP = new HashMap();
static {
SENDER_KEY_MAP.put("name", "sender_key");
SENDER_KEY_MAP.put("required", false);
SENDER_KEY_MAP.put("type", "SenderKeyGroupMessage");
}
}