【发布时间】:2016-05-13 09:17:07
【问题描述】:
我正在开发一个 Android 应用程序,它使用 Google App Engine (GAE) 作为他的后端,用几句话来说情况就是......
在后端(App Engine Java Endpoints Module)我有一些@Entity 类,例如“Vehicle”、“Car”和“SportCar”,其中“SportCar”是“Car”和“Car”的 @Subclass " 是 "Vehicle" 的 @Subclass。
@Entity
public class Vehicle{
@Id
private Long id;
private String brand;
}
@Subclass(index = true)
public class Car extends Vehicle{
private int wheelsNumber;
}
@Subclass(index = true)
public class SportCar extends Car{
private boolean hasTurbo;
}
还有一个端点类“VehicleEndpoint”,其中包含一个返回车辆实体列表的方法。
@Api{name = "vehicleEndpoint", version = "v1", resource = "vehicle", ...}
public class VehicleEndpoint {
@ApiMethod(name = "listVehicles", path = "vehicle",
httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Vehicle> listVehicles(){
...
}
}
所以,当我从客户端(Android 应用程序)调用“listVehicle”时,我收到了 Vehicle 对象的列表,但不知道哪个是“Car”,哪个是“SportCar”。 我无法将这些车辆对象转换为 Car 或 SportCar,因为客户端无法识别子类,因为它们不包含在“VehicleEndpoint”类中。
List<Vehicle> listVehicle = vehicleEndpoint.listVehicles().execute().getItems();
我注意到的另一件事是,在 android 端,我们导入了 Vehicle 类,但它来自另一个包,而不是后端的同一个包(后端包 ->“com.my .example.backend.entity”和客户端导入->“com.my.example.backend.endpoint.vehicleEndpoint.model.Vehicle”)。
我们如何在客户端使用多态性? (总是希望返回最抽象的形式或绝对父级(在本例中为 Vehicle),而不是在客户端检查它是什么类型并使用它)
提前致谢。
【问题讨论】:
标签: java android google-app-engine objectify