简体   繁体   English

使用构造函数中位于同一类中的方法设置变量是否“明智”

[英]Is it 'wise' to set variables with methods in the constructor that are located in the same class

I don't know how to phrase my question any better in the small space I had. 我不知道如何在我的小空间里更好地表达我的问题。 Here's a better explanation: 这是一个更好的解释:

I have a String[] test with a length of 5. The class is used to edit test to be editable in another class. 我有一个长度为5的String[] test 。该类用于编辑test以在另一个类中编辑。 Anyways my constructor passes in all 5 strings to be set to test . 无论如何,我的构造函数传递了所有5个字符串来设置为test I originally had it set up like so: 我最初的设置是这样的:

public Generic(String field1, String field2, String field3, String field4, String field5){
test[0] = field1;
test[1] = field2;
test[2] = field3;
test[3] = field4;
test[5] = field5;
}

But I found I wanted to use the methods I have in the class to set them instead. 但我发现我想使用我在课堂上的方法来设置它们。 So instead of test[0] = field1; 所以代替test[0] = field1; I use setField1(field1); 我用setField1(field1); , etc. All I have in setField1(String Field) is the same thing that used to be in the constructor. 我在setField1(String Field)中的所有内容与之前在构造函数中的内容相同。

I just wanted to make sure that this isn't a dumb move to make, calling methods in my class in the constructor to set a variable. 我只是想确保这不是一个愚蠢的举动,在构造函数中调用我的类中的方法来设置变量。 Or if I shouldn't employ that habit. 或者,如果我不应该养成这种习惯。 Or my thinking is fine. 或者我的想法很好。

Thanks! 谢谢!

The only reason to use a setter in this scenario is if the setter has validation logic. 在这种情况下使用setter的唯一原因是setter是否具有验证逻辑。 Otherwise, you're just adding ever so slight overhead to the JVM by calling a method that is otherwise identical to this.x = x; 否则,您只需通过调用与this.x = x;相同的方法向JVM添加如此轻微的开销this.x = x; .

My first time answering a question, so here goes nothing. 我第一次回答问题,所以这里什么都没有。 The phrasing of your question was difficult to understand btw. 你的问题的措词难以理解。

You should use setter methods in case you need to change only one item in an array. 如果只需要更改数组中的一个项,则应使用setter方法。 Also, your constructor should probably utilize a loop rather than setting each one individually in case your array size is altered in the future. 此外,您的构造函数应该使用循环而不是单独设置每个循环,以防将来更改数组大小。

Also, if you're attempting to edit these values (after I'm assuming they've been set once, constructor or otherwise) from another class as if you've stated, you'll need to use the setters if the variable is set to private. 此外,如果您尝试编辑这些值(在我假设它们已被设置一次,构造函数或其他方式之后),则如同您所述,您将需要使用setter,如果变量是设为私人。

Generally speaking calling methods in your constructor to initialize variables is a good practice. 一般来说,在构造函数中调用方法来初始化变量是一种很好的做法。 It would be even better if those methods are not used except in the constructor to make a private method that does the work and just call this one method in the constructor or split it up to several private functions if they are not going to be used outside that class. 如果不使用那些方法,除了在构造函数中创建一个私有方法来完成工作并在构造函数中调用这个方法,或者如果它们不会在外面使用它们将它分成几个私有函数,那会更好那个班。 So I would have something like the following: 所以我会有如下内容:

public Generic(String field1, String field2, String field3, String field4, String field5) {
    initialize_fields(field1, field2, field3, field4, field5);
}

private void initialize_fields(String field1, String field2, String field3, String field4, String field5) {
    test[0] = field1;
    test[1] = field2;
    test[2] = field3;
    test[3] = field4;
    test[4] = field5;
}

This is following encapsulation that you make the methods that are not used except within the class private (You hide the details of your class and provide a simple interface for the user of your class to follow). 这是以下封装,您将使用除了私有类之外未使用的方法(您隐藏类的详细信息并为您的类的用户提供一个简单的接口)。

Another side is that it's probably a good idea to use an array as input to the constructor rather than 5 values and use a loop to assign the values to the test array. 另一方面,使用数组作为构造函数的输入而不是5个值并使用循环将值分配给测试数组可能是个好主意。

Using setters is also almost always a good idea if you need to check on a condition before setting a value in the setter method, meaning you have validations or conditions that have to be checked in the setter method it is not just assigning. 如果您需要在setter方法中设置值之前检查条件,使用setter也几乎总是一个好主意,这意味着您必须在setter方法中检查它不仅仅是分配的验证或条件。 Also you have to use them if this is not used inside the class otherwise it doesn't matter and it's your preference to go for what you want meaning you can use direct assigning or setters as you wanted to do. 如果在课堂内没有使用它,你也必须使用它们,否则它无关紧要,你喜欢你想要的东西,这意味着你可以按照自己的意愿使用直接分配或设置器。

Using methods to initialize your variables isn't a bad thing by itself, but you shouldn't call methods that could be overridden from your constructor. 使用方法初始化变量本身并不是一件坏事,但是你不应该调用可以从构造函数中重写的方法。

See this answer for more detail: Why is it considered bad practice to call a method from within a constructor? 有关更多详细信息,请参阅此答案: 为什么从构造函数中调用方法被认为是不好的做法?

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

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