我有许多不同的模式,但是每个模式都包含一组字段。我想知道是否有办法让其他架构扩展父架构并继承其字段。例如,这就是我想要的:
message Parent {
required string common1 = 0;
optional string common2 = 1;
}
message Child1 { // can we extend the Parent?
// I want common1, common2 to be fields here
required int c1 = 2;
required string c2 = 3;
}
message Child2 { // can we extend Parent?
// I want common1, common2 to be fields here
repeated int c3 = 2;
repeated string c4 = 3;
}
这样,Child1和Child2还包含来自Parent的common1和common2字段(可能还有更多)。
这有可能吗?
最佳答案
这不是您所提问题的确切答案,但是我们可以做这样的事情来共享通用参数。
message Child1 {
required int c1 = 2;
required string c2 = 3;
}
message Child2 {
required int c1 = 2;
required string c2 = 3;
}
message Request {
required string common1 = 0;
optional string common2 = 1;
oneof msg { Child1 c1 = 2; Child2 c2 = 3; }
}
另一种选择是使用扩展关键字
message Parent {
required string common1 = 0;
optional string common2 = 1;
}
message Child1 {
extend Parent
{
optional Child1 c1 = 100;
}
required int c1 = 2;
required string c2 = 3;
}
关于inheritance - 扩展Protobuf消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29263507/