简体   繁体   English

如何在java中设置公共静态最终类实例的值

[英]how to set the values for a public static final class instance in java

example例子

public static final class MyClass{
        private long id_;
        public static final int MEMBER_ID_FIELD_NUMBER = 2;
        private long startDate_;
    ....

    }

//meanwhile in another class
class Abc {
        ...
    public final void aFunc {
        MyClass aaa = ???? // here is what I want to know how to set the class values
    ....
    }

}

How can I set the value for MyClass in the other class?如何在另一个类中设置MyClass的值? I tried to do "initialize" like我试图做“初始化”之类的

MyClass aaa = {
                id_: 123123132,...
            };

it just give me error that id_ `can't resolve symbol.它只是给我错误 id_ `无法解析符号。

I come from javascript background, that's why I think like this.我来自 javascript 背景,这就是为什么我这样想。 Any idea how can I initiate the instance with partial / all custom default values for a public static final class ?知道如何使用public static final class部分/所有自定义默认值启动实例吗? Btw, if the back-story can help get a full picture --- I do this for testing a specific class's method which is including an instance of MyClass .顺便说一句,如果背景故事可以帮助了解全貌——我这样做是为了测试包含MyClass实例的特定类的方法。 This is an attempt to mock the values instead having to recreate a whole ActionBean to just test one method.这是一种模拟值的尝试,而不必重新创建整个 ActionBean 来仅测试一种方法。

If it has default constructor (the one without any argument) you can first create object without providing fields values and the set it with setters (if they exist) like so:如果它具有默认构造函数(没有任何参数的构造函数),您可以先创建对象而不提供字段值,然后使用 setter(如果存在)设置它,如下所示:

public final void aFunc {
        MyClass aaa = new MyClass();
        a.setId_(1);
        ...
}

If it doesn't have default constructor you need to provide all parameters during object creation, like so:如果它没有默认构造函数,则需要在对象创建期间提供所有参数,如下所示:

public final void aFunc {
        MyClass aaa = new MyClass(1,2,3);
        ....
}

There are multiple ways to create an object instance and assign values to fields:有多种方法可以创建对象实例并为字段赋值:

  1. Call setter methods:调用 setter 方法:

     public static final class MyClass { private long id_; public void setId(long id) { this.id_ = id; } }
     MyClass aaa = new MyClass(); aaa.setId(123123132);
  2. Use a constructor with arguments:使用带参数的构造函数:

     public static final class MyClass { private long id_; public MyClass(long id) { this.id_ = id; } }
     MyClass aaa = new MyClass(123123132);
  3. Use public fields.使用公共字段。 Although not recommended, this is what JavaScript kind of does:虽然不推荐,但这就是 JavaScript 所做的:

     public static final class MyClass { public long id_; }
     MyClass aaa = new MyClass(); aaa.id_ = 123123132;
  4. Use a builder:使用构建器:

     public static final class MyClass { private long id_; private MyClass(long id) { this.id_ = id; } public static class Builder { private long id_; public void id(long id) { this.id_ = id; } public MyClass build() { MyClass x = new MyClass(); x.id_ = this.id_; return x; } } }
     MyClass aaa = new MyClass.Builder() .id(123123132) .build();

You have two options in order to fill the fields with values.您有两个选项可以用值填充字段。 One options is using the public constructor passing all the parameters you want to update:一种选择是使用公共构造函数传递您要更新的所有参数:

public static final class MyClass{
    private long id_;
    public static final int MEMBER_ID_FIELD_NUMBER = 2;
    private long startDate_;

    public MyClass(long id_, long startDate_) {
        this.id_ = id_;
        this.startDate_ = startDate_;
    }
}

You can use it as bellow:您可以按如下方式使用它:

public final void aFunc {
    MyClass objectA = new MyClass(1L, 23214112213L);
}

Second option is using setters: method per field to add value:第二个选项是使用 setters: method per field 添加值:

public static final class MyClass{
    private long id_;
    public static final int MEMBER_ID_FIELD_NUMBER = 2;
    private long startDate_;
    
    public void setID(long id_) {
         this.id_ = id_;
    }

    public void setStartDate(long startDate_) {
         this.startDate_ = startDate_;
    }
        
}

You can use them as bellow:您可以按如下方式使用它们:

public final void aFunc {
    MyClass objectB = new MyClass();
    objectB.setID(1L);
    objectB.setStartDate(23214112213L);
}

But you can combine them.但是你可以将它们结合起来。 You can have the constructor and bellow the setters.您可以拥有构造函数并使用 setter。

See java constructor and see setters and getters查看java 构造函数并查看setter 和 getter

  • For you private fields i assume you need getters too.对于您的私人领域,我假设您也需要吸气剂。

Those are part of the basic knowledge you should have.这些是您应该具备的基本知识的一部分。 In addition keep in mind there are conventions (naming etc)另外请记住有约定(命名等)

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

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