我正在进行从C ++到Java的转换。 有谁知道如何将其转换为Java?
typedef struct {
int id;
char *old_end;
char *new_end;
int old_offset;
int new_offset;
int min_root_size;
int func;
} RuleList;
static RuleList step1a_rules[] =
{
101, "sses", "ss", 3, 1, -1, _NULL,
102, "ies", "i", 2, 0, -1, _NULL,
103, "ss", "ss", 1, 1, -1, _NULL,
104, "s", _LAMBDA, 0, -1, -1, _NULL,
000, NULL, NULL, 0, 0, 0, _NULL,
};
感谢
答案 0 :(得分:1)
您需要RuleList
的构造函数:
class RuleList {
int id;
String old_end;
String new_end;
int old_offset;
int new_offset;
int min_root_size;
int func;
RuleList(int id, String old_end, String new_end, int old_offset,
int new_offset, int min_root_size, int func)
{
this.id = id;
this.old_end = old_end;
// etc.
}
};
static RuleList[] step1a_rules = {
new RuleList(101, "sses", "ss", 3, 1, -1, _NULL),
new RuleList(102, "ies", "i", 2, 0, -1, _NULL),
new RuleList(103, "ss", "ss", 1, 1, -1, _NULL),
new RuleList(104, "s", _LAMBDA, 0, -1, -1, _NULL),
new RuleList(000, null, null, 0, 0, 0, _NULL),
};
这假定_NULL
是定义的静态int
值,_LAMBDA
是定义的静态String
值。
答案 1 :(得分:1)
在Java中执行此操作的标准方法如下。 Java中的RuleList类比RuleList C结构更冗长,但有一些简单的方法可以处理它,例如使用Eclipse生成大部分代码。
public class RuleList {
private final int id;
private final String oldEnd;
private final String newEnd;
private final int oldOffset;
private final int newOffset;
private final int minRootDize;
private final int func;
public RuleList(int id, String oldEnd, String newEnd, int oldOffset,
int newOffset, int minRootDize, int func) {
this.id = id;
this.oldEnd = oldEnd;
this.newEnd = newEnd;
this.oldOffset = oldOffset;
this.newOffset = newOffset;
this.minRootDize = minRootDize;
this.func = func;
}
public int getId() {
return id;
}
public String getOldEnd() {
return oldEnd;
}
public String getNewEnd() {
return newEnd;
}
public int getOldOffset() {
return oldOffset;
}
public int getNewOffset() {
return newOffset;
}
public int getMinRootDize() {
return minRootDize;
}
public int getFunc() {
return func;
}
}
RuleList[] step1aRules = new RuleList[] {
new RuleList(101, "sses", "ss", 3, 1, -1, 0),
new RuleList(102, "ies", "i", 2, 0, -1, 0),
new RuleList(103, "ss", "ss", 1, 1, -1, 0),
new RuleList(104, "s", _LAMBDA, 0, -1, -1, 0),
new RuleList(000, null, null, 0, 0, 0, 0),
};
答案 2 :(得分:0)
您可以创建一个类RuleList,然后创建此类的数组:
public class RuleList{
int id ;
char old_end; //note that java does not use (* for pointer like C++)
char new_end;
int old_offset;
}
static RuleList[] step1a_rules = new RuleList[]{
new RuleList (101,"sses","ss",3, 1,-1, 0),... };
在上面的代码中,您将看到与C ++不同的东西,因为使用Java,几乎所有东西都是对象。所以你声明step1a_rules是一个包含其他对象的数组(Rulelist
)。因此,对于此类的每个对象,必须使用关键字new
来实例化新对象。
@ edit:啊,我几乎忘了:你应该注意到,java和C ++有一些不同:在C ++类中,默认成员是私有的。在Java类中,默认成员是public。当然,上面的代码没有问题,因为struct的默认成员也是公共的:D