我有以下结构,并且在给定ID值时需要获取相应的标记名称(对象名称)。
理想情况下,我会有一个函数:enum class Color { red, green };
struct Tag {
uint8_t id[4];
Color color;
};
Tag RED1 = { { 0x73, 0xD5, 0xB7, 0xAC }, Color::red } ;
Tag RED2 = { { 0x7E, 0x27, 0x49, 0x4E }, Color::red } ;
Tag RED3 = { { 0x02, 0xFD, 0x06, 0x40 }, Color::red } ;
Tag GREEN1 = { { 0xAB, 0xEC, 0x68, 0x80 }, Color::green } ;
Tag GREEN2 = { { 0xEE, 0x20, 0x50, 0x4E }, Color::green } ;
Tag GREEN3 = { { 0x27, 0x06, 0x40, 0x73 }, Color::green } ;
返回名称。
e.g。 GetName(0x73,0xD5,0xB7,0xAC)将返回标签RED1
public class Main {
static class TestDataProvider {
public String getString(){
return "Data provider. Test String";
}
public int getInteger() {
return 1510872341;
}
public double getReal() {
return 34.6729;
}
}
interface DataGetterType <T> {
T getData();
}
static class Column <T> {
public String getName() {
return nameTag;
}
public String getDataType(){
return dataTypeTag;
}
public T getData(){
return getter.getData();
}
public Column(String name, String type, DataGetterType<T> getterObj){
nameTag = name;
dataTypeTag = type;
getter = getterObj;
}
private final String nameTag;
private final String dataTypeTag;
private final DataGetterType<T> getter;
}
static class StringColumn extends Column<String> {
StringColumn(String name, TestDataProvider provider) {
super(name, "TEXT", provider::getString);
}
}
static class IntegerColumn extends Column<Integer> {
IntegerColumn(String name, TestDataProvider provider) {
super(name, "INTEGER", provider::getInteger);
}
}
static class RealColumn extends Column<Double> {
RealColumn(String name, TestDataProvider provider) {
super(name, "REAL", provider::getReal);
}
}
public static void main(String[] args) {
TestDataProvider myProvider = new TestDataProvider();
StringColumn modelColumn = new StringColumn("model", myProvider);
IntegerColumn dateColumn = new IntegerColumn("year_of_production", myProvider);
RealColumn weightColumn = new RealColumn("weight", myProvider);
System.out.print(modelColumn.getDataType() + " " + modelColumn.getName() + ": " + modelColumn.getData() + ";\n" +
dateColumn.getDataType() + " " + dateColumn.getName() + ": " + dateColumn.getData() + ";\n" +
weightColumn.getDataType() + " " + weightColumn.getName() + ": " + weightColumn.getData() + ";\n");
}