我正在开发使用谷歌的App Engine(GAE)为他的后端和与几个字情况的Android应用程序是...的Android GAE多态性在客户端
在后端侧( App Engine的Java的端点模块)我有几个@Entity类,如“汽车”,“汽车”和“SportCar”,其中“SportCar”是“汽车”和“汽车”的@Subclass是“车”的@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”我收到了车载物的清单,但不知道这是“汽车”,哪些是“SportCar”。 因为子类不是在客户端的认可,我不能投这些车载物到汽车或SportCar,因为他们没有在“VehicleEndpoint”类包括。
List<Vehicle> listVehicle = vehicleEndpoint.listVehicles().execute().getItems();
和我注意到另一件事是,在Android方面,我们进口汽车类,但它是从另一个包,不一样的包,它是在后端侧(后端封装 - > “com.my.example.backend.entity”和客户端导入 - >“com.my.example.backend.endpoint.vehicleEndpoint.model.Vehicle”)。
如何,我们可以使用在客户端的多态性? (总是希望回到最抽象的形式或绝对父(在这种情况下,汽车),比客户端检查它是什么样的,并用它)提前
感谢。
首先感谢您的回复。但是,例如,如果其中一个子类“Car”未包含在端点类“VehicleEndpoint”中,那么我们无法在客户端访问它,因为此子类不会添加到库的包“com”中。 my.example.backend.endpoint.vehicleEndpoint”。 – MrVasilev
另外我想在这里添加的是我不认为这里最好的方法是直接在前端使用来自后端的实体类。可能是在前端创建一个单独的实体类并接收来自后端的JSON数据以便在前端解析这些实体会很好吗? – MrVasilev