将带有接口字段的类转换为Protobuf。

huangapple 未分类评论50阅读模式
英文:

Convert class with interface field to protobuf

问题

  1. syntax = "proto3";
  2. message A {
  3. string name = 1;
  4. string occupation = 2;
  5. oneof operable {
  6. OperableA operable_a = 3;
  7. OperableB operable_b = 4;
  8. }
  9. }
  10. message OperableA {
  11. string job = 1;
  12. repeated string tasks = 2;
  13. }
  14. message OperableB {
  15. int32 number_of_workers = 1;
  16. repeated string skills = 2;
  17. repeated string name_of_workers = 3;
  18. }
英文:

I have a class with interface field and the interface has multiple implementations. I need to convert my class to protobuf, but I am not able to map the interface field, which is actually a type interface.
Here is my Java code for class structure:

  1. class A {
  2. String name;
  3. String occupation;
  4. Operable operable;
  5. }
  6. interface operable {
  7. }
  8. class OperableA implements operable {
  9. String job;
  10. List<String> tasks;
  11. }
  12. class OperableB implements operable {
  13. int numberOfWorkers;
  14. List<String> skills;
  15. List<String> nameOfWorkers;
  16. }

How can this structure be mapped in proto file, please help.

答案1

得分: 0

你不能在 Protocol Buffers 中像 这里 所描述的那样对类或接口进行继承建模,文档中提到:

> 不要寻找类继承相似的功能 - Protocol Buffers 并不支持这样的特性。

英文:

You cannot model class or interface inheritance in protobuf as described here which says

> Don't go looking for facilities similar to class inheritance, though – protocol buffers don't do that.

huangapple
  • 本文由 发表于 2020年7月24日 15:19:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/63068698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定