简体   繁体   English

在Java中创建和初始化对象

[英]creating and initializing object in java

My question is very simple and novice. 我的问题很简单而且是新手。 I have a class, and want to create instance of it. 我有一个类,并想创建它的实例。 So should I use temporary variables and then assign the values using setters or should I create an object and then directly assign user input to setter. 因此,我应该使用临时变量,然后使用设置器分配值,还是应该创建一个对象,然后将用户输入直接分配给设置器。

class A {
int a;

A(){
    a=0;
}

A(int a){
    this.a=a;
}

} }

Now how should I create objects? 现在我应该如何创建对象?

  1. Should I use temporary variables and then use constructor to create object. 我应该使用临时变量,然后使用构造函数创建对象。
  2. Should I create object and directly assign user input to instance variable. 我应该创建对象并将用户输入直接分配给实例变量。 for example 例如
    aObj.setA(getUserInput());

Please advice. 请指教。

To the first question: Either. 对于第一个问题:可以。 Depends on a lot of things like: 取决于很多事情,例如:

  • Do you already know what the initial value or default value should be? 您已经知道初始值或默认值应该是什么吗?
  • Would the class work properly/improperly without it set to anything at first? 如果一开始没有设置任何内容,该类是否可以正常工作?
  • Should the internal variable even have a setter at all? 内部变量是否甚至应该有一个setter?

Example: if I had 示例:如果我有

class bonkinator {
int timesBonked;

bonk(){
  timesBonked++;
}

getBonks(){
  return timesBonked; // thanks for the correction, Unihedron
}

...

Then chances are I wouldn't want other code to meddle with timesBonked, so I wouldn't give it a setter. 然后可能是我不希望其他代码与timesBonked混在一起,所以我不会给它一个二传手。 So I'd want to set timesBonked to some initial value in the constructor, and whether or not that value is something I choose or something the calling code chooses depends on what we're trying to do. 因此,我想在构造函数中将timesBonked设置为某个初始值,并且该值是否是我选择的值或调用代码选择的值取决于我们要执行的操作。

To the second question: depends again. 第二个问题:再次依赖。

  • Do you plan on re-using that thing from getUserInput()? 您打算重新使用getUserInput()中的东西吗?
  • Would calling getUserInput() either annoy the user or re-run slow code (eg try to read something from a database that hasn't changed) for no added benefit? 调用getUserInput()会否激怒用户或重新运行慢速代码(例如,尝试从未更改的数据库中读取内容),而不会带来额外的好处?

Invoke new A(); 调用new A(); or new A(42); new A(42); in the same package. 在同一包中。

Please see the following: 请参阅以下内容:

A a = new A(getUserInput());

A a = new A();
a.a = getUserInput();

There is not much difference but choose your preference in terms of code style: The Java compiler will optimize most of it. 差别不大,但是可以根据代码样式选择首选项:Java编译器将对其中的大部分进行优化。

That totally depends on the design but generally 2nd way is preferred in production code because you might add new variables in the class later and then if you change the constructor you might break existing code. 这完全取决于设计,但是通常在生产代码中首选第二种方式,因为您稍后可能会在类中添加新变量,然后如果更改构造函数,则可能会破坏现有代码。

Also

A(){
    a=0;
}

is not necessary as instance variables get initialized with default values ie 0 for int. 不需要实例变量,因为实例变量使用默认值初始化,即int为0。

There is no specific way to do that. 没有具体的方法可以做到这一点。 It is purely based on what you wanna do. 它完全基于您想做的事。 You can either have a setter to set the value after initializing the instance. 您可以在初始化实例后使用设置器来设置值。 Or you can have a parameter constructor and pass the value at the initializing point. 或者,您可以具有参数构造函数,然后在初始化点传递值。 Or even you can have fixed value if you want at the initializing. 甚至在初始化时也可以具有固定值。 It is entirely up to you to decide what is the best approach for you to what you are trying to do. 完全由您决定什么才是您尝试执行的最佳方法。

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

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