如何在Java中为公共的静态不可变类实例设置值。

huangapple go评论99阅读模式
英文:

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

问题

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
    ....
    }
}

如何在另一个类中为MyClass设置值?我尝试进行“初始化”操作,就像这样:

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

但是它只是给我一个“无法解析符号id_”的错误。我来自JavaScript背景,所以我这样想。有任何方法可以为public static final class的实例初始化部分/全部自定义默认值吗?顺便说一下,如果背景故事有助于获取全面的情况 --- 我这样做是为了测试特定类的方法,该方法包括MyClass的实例。这是一种尝试,通过模拟这些值,而不必重新创建整个ActionBean来测试一个方法。

英文:

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? I tried to do "initialize" like

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

it just give me error that id_ `can't resolve symbol.

I come from javascript background, that's why I think like this. Any idea how can I initiate the instance with partial / all custom default values for a 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. This is an attempt to mock the values instead having to recreate a whole ActionBean to just test one method.

答案1

得分: 1

如果它有默认构造函数(即没有任何参数的构造函数),您可以首先创建对象,而无需提供字段值,然后使用设置器(如果存在)进行设置,如下所示:

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

如果它没有默认构造函数,则需要在对象创建过程中提供所有参数,如下所示:

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

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:

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);
        ....
}

答案2

得分: 1

有多种方法可以创建对象实例并为字段赋值:

  1. 调用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. 使用带参数的构造函数:

    public static final class MyClass {
        private long id_;
    
        public MyClass(long id) {
            this.id_ = id;
        }
    }
    
    MyClass aaa = new MyClass(123123132);
    
  3. 使用公共字段。虽然不推荐,但类似于JavaScript的做法:

    public static final class MyClass {
        public long id_;
    }
    
    MyClass aaa = new MyClass();
    aaa.id_ = 123123132;
    
  4. 使用构建器(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();
    
英文:

There are multiple ways to create an object instance and assign values to fields:

  1. Call setter methods:

    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:

    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();
    

答案3

得分: 1

你有两个选项来填充字段的值。
一个选项是使用公共构造函数传递所有要更新的参数:

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_;
    }
}

你可以按以下方式使用它:

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

第二个选项是使用设置器:为每个字段添加值的方法:

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_;
    }
}

你可以按以下方式使用它们:

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

但你也可以将它们结合起来。你可以有构造函数,然后是设置器。

参考Java构造函数
参考设置器和获取器

  • 对于你的私有字段,我假设你也需要获取器。

这些是你应该掌握的基础知识的一部分。另外要记住有一些约定(命名等)。

英文:

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:

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.

See java constructor and
see setters and getters

  • 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)

huangapple
  • 本文由 发表于 2020年10月12日 23:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64320731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定