是否可以将引用类型传递给需要动态引用类型的对象实例?

例如:


public void builCarManager(Car car) {

   Manager<car.getClass()> carManager = new Manager<>();

}

解决这个问题的一些建议?


编辑


我的主要问题是:


public class CustomSerializer<T> extends StdSerializer<T> {


    @Override

    public void serialize(T car, JsonGenerator gen, SerializerProvider sp) {

        // I get car.getClass() fine but I need add it

        Manager<T> carManager = new Manager<>();

        // Manager<car.getClass()> carManager = new Manager<>();

    }

}

这里我不能通过CustomSerializer<Lamborgini>.class。


@JsonSerialize(using = CustomSerializer.class)

private Lamborgini car; 


阿波罗的战车
浏览 104回答 3
3回答

慕尼黑5688855

假设您有以下POJO模型:class Car {}class Lamborghini extends Car {}class Ferrari extends Car {}您的Manager课程如下所示:class Manager<T extends Car> {&nbsp; &nbsp; private Class<T> clazz;&nbsp; &nbsp; public Manager(Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; this.clazz = clazz;&nbsp; &nbsp; }&nbsp; &nbsp; public String generateCustomCarName() {&nbsp; &nbsp; &nbsp; &nbsp; if (Lamborghini.class == clazz) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Lambooooo!";&nbsp; &nbsp; &nbsp; &nbsp; } else if (Ferrari.class == clazz) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Wild horse Ferrari!";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return "Not for everyone!";&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return String.format("Manager[clazz=%s]", clazz.getName());&nbsp; &nbsp; }}现在,让我们创建一个Store有 oneFerrari和 one的Lamborghini:class Store {&nbsp; &nbsp; private Car ferrari = new Ferrari();&nbsp; &nbsp; private Car lamborghini = new Lamborghini();&nbsp; &nbsp; public Car getFerrari() {&nbsp; &nbsp; &nbsp; &nbsp; return ferrari;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFerrari(Car ferrari) {&nbsp; &nbsp; &nbsp; &nbsp; this.ferrari = ferrari;&nbsp; &nbsp; }&nbsp; &nbsp; public Car getLamborghini() {&nbsp; &nbsp; &nbsp; &nbsp; return lamborghini;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLamborghini(Car lamborghini) {&nbsp; &nbsp; &nbsp; &nbsp; this.lamborghini = lamborghini;&nbsp; &nbsp; }}现在,我们需要class在运行时获取信息。由于Java's Type Erasure我们需要提供class:class CustomCarSerializer<T extends Car> extends StdSerializer<T> {&nbsp; &nbsp; public CustomCarSerializer(Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; super(clazz);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)&nbsp; &nbsp; &nbsp; &nbsp; throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; Manager<T> manager = new Manager<>(_handledType);&nbsp; &nbsp; &nbsp; &nbsp; jgen.writeString(manager.generateCustomCarName());&nbsp; &nbsp; }}请注意,它StdSerializer也有需要的构造函数,class我们可以稍后使用_handledType. 现在我们需要使用序列化器配置我们的实现SimpleModule:import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.SerializerProvider;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.ser.std.StdSerializer;import java.io.IOException;public class Cars {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule carsModule = new SimpleModule("CarsModule");&nbsp; &nbsp; &nbsp; &nbsp; carsModule.addSerializer(Lamborghini.class, new CustomCarSerializer<>(Lamborghini.class));&nbsp; &nbsp; &nbsp; &nbsp; carsModule.addSerializer(Ferrari.class, new CustomCarSerializer<>(Ferrari.class));&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(carsModule);&nbsp; &nbsp; &nbsp; &nbsp; mapper.enable(SerializationFeature.INDENT_OUTPUT);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(new Store()));&nbsp; &nbsp; }}上面的代码打印:{&nbsp; "ferrari" : "Wild horse Ferrari!",&nbsp; "lamborghini" : "Lambooooo!"}

人到中年有点甜

处理这个问题的正确方法是让两个类(例如Car和Desk)实现一个公共接口(例如Item)并将该接口与Manager该类相关联。public class Instance {&nbsp; &nbsp; private static interface Item {&nbsp; &nbsp; &nbsp; &nbsp; String getName();&nbsp; &nbsp; }&nbsp; &nbsp; private static class Car implements Item {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Car";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static class Desk implements Item {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Desk";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static class Manager<T extends Item> {&nbsp; &nbsp; &nbsp; &nbsp; private T item;&nbsp; &nbsp; &nbsp; &nbsp; public Manager(T item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.item = item;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return String.format("Manager[item=%s]", item.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static <E extends Item> Manager<E> buildItemManager(E item) {&nbsp; &nbsp; &nbsp; &nbsp; return new Manager<E>(item);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Item car&nbsp; = new Car(), desk = new Desk();&nbsp; &nbsp; &nbsp; &nbsp; Manager<Item> manager1 = buildItemManager(car);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(manager1);&nbsp; &nbsp; &nbsp; &nbsp; Manager<Item> manager2 = buildItemManager(desk);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(manager2);&nbsp; &nbsp; }}输出Manager[item=Car]Manager[item=Desk]另一种方法...Manager通过传入一个代表 a 的类来实例化 a Item。public class Instance {&nbsp; &nbsp; public static interface Item {&nbsp; &nbsp; &nbsp; &nbsp; String getName();&nbsp; &nbsp; }&nbsp; &nbsp; public static class Car implements Item {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Car";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class Desk implements Item {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Desk";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class Manager<T extends Item> {&nbsp; &nbsp; &nbsp; &nbsp; private T item;&nbsp; &nbsp; &nbsp; &nbsp; public Manager(T item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.item = item;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return String.format("Manager[item=%s]", item.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static <E extends Item> Manager<E> buildItemManager(Class<E> item) throws InstantiationException, IllegalAccessException {&nbsp; &nbsp; &nbsp; &nbsp; return new Manager<E>(item.newInstance());&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Cannot initialize array with generic types... */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Manager<? extends Item>[] managers = new Manager[2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; managers[0] = buildItemManager(Car.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; managers[1] = buildItemManager(Desk.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Manager<?> manager : managers) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(manager);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (InstantiationException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

qq_笑_17

是的,您可以使用泛型。&nbsp; public <T> void builCarManager(T car) {&nbsp; &nbsp; Manager<T> carManager = new Manager<>();&nbsp;}现在您可以传递任何类型的对象。创建的管理器将属于同一类型。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java