首页 > 解决方案 > 将具有接口字段的类转换为 protobuf

问题描述

我有一个带有接口字段的类,并且该接口有多个实现。我需要将我的类转换为 protobuf,但我无法映射接口字段,这实际上是一个类型接口。这是我的类结构的Java代码:

class A {
   String name;
   String occupation;
   Operable operable;
}
interface operable {
}
class OperableA implements operable {
    String job;
    List<String> tasks; 
}
class OperableB implements operable {
    int numberOfWorkers;
    List<String> skills;
    List<String> nameOfWorkers;
}

这个结构如何映射到 proto 文件中,请帮忙。

标签: javagrpcgrpc-javaprotoprotobuf-java

解决方案


您不能在 protobuf 中对类或接口继承建模,如此处所述

不过,不要去寻找类似于类继承的工具——协议缓冲区不会那样做。


推荐阅读