简体   繁体   English

使用getter / setter实现嵌套的抽象类

[英]implement nested abstract class with getter/setter

I have the following abstract class structure: 我有以下抽象类结构:

public abstract class A {
    ...
    private List<B> b;

    public List<B> getB() {
        return b;
    }
    public void setB(List<B> b) {
        this.b = b;
    }
    public static abstract class B {

    }
}

and implement it like this: 并像这样实现它:

public class AA extends A {
    public static class BB extends B {
        ...
    }

When I now use the jackson to map Json on AA, I get an error, that it could not create an instance of A$B. 当我现在使用jackson在AA上映射Json时,我收到一个错误,它无法创建A $ B的实例。 I think this is, because the getter/setter in A still references on B and not on BB, which causes the error. 我认为这是因为A中的getter / setter仍然引用B而不是BB,这会导致错误。 Is there any way I can do it like that, without also putting the getter/setter in the subclass? 有没有办法我可以这样做,而不是将getter / setter放在子类中?

You could add type capture to make sure b is always correctly typed like this: 您可以添加类型捕获以确保b始终正确键入,如下所示:

public abstract class A<T extends A.B> {
    ...
    private List<T> b;

    public List<T> getB() {
        return b;
    }
    public void setB(List<T> b) {
        this.b = b;
    }
    public static abstract class B {
        ...
    }
}

and then 然后

public class AA extends A<AA.BB> {
    ...
    public static class BB extends B {
        ...
    }

There are several options how to de-serialize. 如何反序列化有多种选择。 If you have no annotations, Jackson probably falls back to using a constructor of nearest superclass that has a public constructor with no parameters. 如果你没有注释,杰克逊可能会回到使用最近的超类的构造函数,该构造函数具有没有参数的公共构造函数。 After creating an empty object, it then sets the fields using reflections. 创建空对象后,它会使用反射设置字段。

  • Reflections WILL bypass any validations in constructor like Objects.requireNotNull . Reflection将绕过Objects.requireNotNull构造函数中的任何验证。

A more refined way how to use Jackson is to use @JsonCreator and @JsonProperty in constructor, naming all the parameters. 如何使用Jackson的更精确的方法是在构造函数中使用@JsonCreator@JsonProperty ,命名所有参数。

  • This enables you to force Jackson to use a constructor you made and validate the object. 这使您可以强制Jackson使用您创建的构造函数并验证该对象。

I am quite unsure about this rather unusual structure. 我对这个相当不寻常的结构非常不确定。 After all, you have an abstract class nested in an abstract class, I have seen a lot of things, but I haven't seen anything like this. 毕竟,你有一个嵌套在抽象类中的抽象类,我已经看到了很多东西,但我还没有看到这样的东西。 Is there a good reason? 有充分的理由吗? Or is it not a problem to just move the inner class to a file of its own? 或者将内部类移动到自己的文件不是问题吗? (it is public static after all) (毕竟是public static

If you create an instance of AA you need an instance of the abstract class B and you define nowwhere, which instance to use. 如果创建AA实例,则需要抽象类B的实例,并定义nowwhere,使用哪个实例。 Just providing some implementation ( BB ) isn't enough. 仅提供一些实现( BB )是不够的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM