简体   繁体   English

AutoValue - 公共构造函数可能

[英]AutoValue - public constructor possible

Is this possible? 这可能吗? Or is the only solution the builder? 或者是建筑商的唯一解决方案? Having a class with 10 fields would mean have to duplicate the 10 fields in the builder so that AutoValue works. 拥有10个字段的类意味着必须复制构建器中的10个字段,以便AutoValue可以工作。 Or via a manual written create function? 或者通过手动编写的创建功能? Or am I missing something? 或者我错过了什么?

I just want to create an object of my AutoValue class outside of the package... 我只是想在包外面创建一个AutoValue类的对象...

Here's a short example: 这是一个简短的例子:

@AutoValue
public abstract class Data
{
    // Can something like this be auto generated????
    public static Data create(String field1, String field2, ...)
    {
        return new AutoValue_Data(field1, field2, ...);
    }

    public abstract String field1();
    public abstract String field2();
    ...

    @AutoValue.Builder
    public abstract static class Builder {
        // Or can I tell the builder to create setters for ALL fields 
        // automatically instead of having to declare them one by one?
        public abstract Builder setField1(String field1);
        public abstract Builder setField2(String field2);
        ...
        public abstract Data build();
    }
}

The create() method is just a wrapper around the constructor to hide implementation details like the class AutoValue_Data itself. create()方法只是构造函数的包装器,用于隐藏类AutoValue_Data本身的实现细节。 If you just don't want to write everything yourself, you can find a plugin for Android Studio for creating these methods automatically. 如果您不想自己编写所有内容,可以在Android Studio中找到一个自动创建这些方法的插件

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

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