为了扩展他的示例,可以通过以下更改使他的适配器类通用以适用于所有类型的对象(不仅仅是IAnimal):class InheritanceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T>{.... public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context).... public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException....}在测试类中:public class Test { public static void main(String[] args) { .... builder.registerTypeAdapter(IAnimal.class, new InheritanceAdapter<IAnimal>()); ....}