简体   繁体   English

未找到构造函数 Java + Lombok

[英]No Constructor Found Java + Lombok

Main主要的

public static List<Viking> uploadVikings(){
        List<Viking> vikings = new ArrayList<Viking>();

        vikings.add(new Viking("Lean",23,100, new DrinkVikingImp(), new PeeVikingImp(),10));
        vikings.add(new Viking("Thor",24,99, new DrinkVikingImp(), new PeeVikingImp(),9));
        vikings.add(new Viking("Thanos",25,98, new DrinkVikingImp(), new PeeVikingImp(),9));
        vikings.add(new Viking("Hulk",26,97, new DrinkVikingImp(), new PeeVikingImp(),5));
        vikings.add(new Viking("Thrall",27,96, new DrinkVikingImp(), new PeeVikingImp(),3));

        return vikings;
    }

Viking Class维京 Class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Viking extends Human{

    public Integer proDrinker;
    public Pee pee;
    public Drink drink;

    public void pee() {pee.pee();}
    public void drink() {drink.drink();}

}

Human Class人类 Class

@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class Human {

    public String Name;
    public Integer Age;
    public Integer Weight;

}

Drink and Pee interface are the same喝水和尿尿的界面是一样的

public class DrinkVikingImp implements Drink {

    @Override
    public void drink() {
        System.out.println("Viking is Drinking");
    }
}
Error: Error:(21, 21) java: no suitable constructor found for Viking(java.lang.String,int,int,com.company.models.DrinkVikingImp,com.company.models.PeeVikingImp,int)
    constructor com.company.models.Viking.Viking() is not applicable
      (actual and formal argument lists differ in length)
    constructor com.company.models.Viking.Viking(java.lang.Integer,com.company.interfaces.Pee,com.company.interfaces.Drink) is not applicable
      (actual and formal argument lists differ in length)

You can solve this problem using Lombok`s builder generator (@SuperBuilder annotation), which was introduced in version 1.18你可以使用 Lombok 的 builder 生成器(@SuperBuilder 注解)来解决这个问题,它是在 1.18 版本中引入的

@Data
@SuperBuilder
public abstract class Human {
...
}

@Data
@SuperBuilder
public class Viking extends Human {
...
}

Then you`ll be able to construct your Viking objects as below:然后你就可以构建你的 Viking 对象,如下所示:

    vikings.add(Viking.builder()
            .name("Lean")
            .age(23)
            .weight(100)
            .drink(new DrinkVikingImp())
            .pee(new PeeVikingImp())
            .proDrinker(10)
            .build());

Also check your Human class field names, they should not start with a capital letter.还要检查您的 Human class 字段名称,它们不应以大写字母开头。

From lombok :龙目岛

@AllArgsConstructor generates a constructor with 1 parameter for each field in your class. @AllArgsConstructor 为 class 中的每个字段生成一个带有 1 个参数的构造函数。

Based on the error message根据错误信息

com.company.models.Viking.Viking(java.lang.Integer,com.company.interfaces.Pee,com.company.interfaces.Drink) is not applicable

You probably need this instead你可能需要这个

vikings.add(10, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(9, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(9, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(5, new Viking(new DrinkVikingImp(), new PeeVikingImp()));
vikings.add(3, new Viking(new DrinkVikingImp(), new PeeVikingImp()));

And then you'll have to set human attributes by yourself然后你必须自己设置人类属性

This is not possible in Lombok.这在龙目岛是不可能的。 Although it would be a really nice feature, it requires resolution to find the constructors of the super class.虽然这将是一个非常好的功能,但它需要解决才能找到超级 class 的构造函数。 The super class is only known by name the moment Lombok gets invoked.超级 class 只有在 Lombok 被调用时才知道名称。 Using the import statements and the classpath to find the actual class is not trivial.使用导入语句和类路径来查找实际的 class 并非易事。 And during compilation you cannot just use reflection to get a list of constructors.在编译期间,您不能只使用反射来获取构造函数列表。

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

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