我正在尝试读取文本纯数据中的一个 block ,并将其分离到结构中的各个属性中。
就像经典示例的 C/C++ 代码一样:
typedef struct {
char yyyy[4],
char mm[2],
char dd[2]
} ISO;
typedef struct {
char dd[2],
char mm[2],
char yyyy[4]
} JAPAN;
typedef struct {
char mm[2],
char dd[2],
char yyyy[4]
} USA;
typedef union {
char date[8],
ISO iso_date,
JAPAN japan_date,
USA usa_date
} date_format;
/////
char date[8] = "20150304";
date_format format = (date_format)date;
printf("%s\n", format->iso_date->yyyy);
如何在 Java 或 Groovy 中表示它?
最佳答案
1) Java 没有结构体。
但是你可以使用像这样的类
class ISO {
public char yyyy = new char[4];
public char mm = new char[2];
//etc..
}
2)用类层次结构替换联合
abstract class DateFormat {
abstract Object getObj();
}
class uISO extends DateFormat {
ISO iso = new ISO();
public Object getObj() { return obj; }
}
其他结构依此类推。
关于java - java中的联合typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858714/