【问题标题】:Difference between constructor and instance variables构造函数和实例变量之间的区别
【发布时间】:2019-05-19 00:28:53
【问题描述】:

我需要了解构造函数和实例变量。我的问题是构造函数是在第 1 行初始化的,而我可以通过在第 2 行创建实例变量来做到这一点。为什么我需要使用构造函数来初始化值,而我可以使用实例变量来初始化值?

class ExampleConstructor{
    int value;
    ExampleConstructor(int value){
        this.value=value;
    }
}
public class Main{
    public static void main(String[] args){
        ExampleConstructor constructor=new ExampleConstructor(100);/*line 1*/
        System.out.println(constructor.value);
        constructor.value=10;/*line 2*/
        System.out.println(constructor.value);
    }
}

【问题讨论】:

  • 你在问为什么你不能只做int value = 10;?因为你能。构造函数只允许您在创建对象时初始化值
  • 你绝对可以做到。只需使用一个空的构造函数。您需要使用构造函数来实例化实例,因为您的类不是静态类,而是从静态方法(main)调用它。
  • 你可以看到它就像生了一些东西,或者创造了它。要创造一些东西,显然你需要成分,否则你的创造将丢失重要的东西。而且你不能在生成它之前编辑创建属性(神奇的new 运算符)。就是这样。
  • 你应该阅读这篇关于封装的文章:stackoverflow.com/questions/1568091/…
  • 我认为您应该从班级名称中删除“构造函数”一词。当您询问有关 Constructor 构造函数的问题时,它只会引起混淆和误解。

标签: java constructor instance-variables


【解决方案1】:

构造函数用于创建您的类的实例。

如果您不关心值被多个代码位置编辑并影响每个人,那么您可以将 value 设置为 static 并在没有构造函数的情况下使用它。理解实例变量和静态变量之间的区别非常重要,因为有些地方静态是有意义的。

在您的情况下,您通过调用其构造函数并将其分配给变量constructor 来创建类ExampleConstructor 的实例。然后将“实例变量”value改为10。你不必将值传递给构造函数,你可以有一个空的构造函数并在实例化对象后设置值。

如果您将成员value 设为static,例如static int value;,并在没有构造函数的情况下使用它,例如ExampleConstructor.value,它就可以工作。但问题是,如果另一段代码将 ExampleConstructor.value 设置为 2828,那么该行之后的每一段代码在 println ExampleConstructor.value 时都会得到 2828。

如果您不希望这种情况发生,那么通过一个地方的代码更改会影响其他地方。然后你应该将成员value定义为实例变量,并使用构造函数来实例化一个对象并使用实例变量。

IMO,您对类和变量的命名很容易让读者感到困惑。

查看下面的代码块以获得更好的解释。

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Printing static value from TestClass.");
        // Observe that the TestClass is not being instantiated to operate on staticValue;
        System.out.println("TestClass.staticValue:  " + TestClass.staticValue);
        changeStaticValue();
        System.out.println("\nPrinting static value from TestClass after editing.");
        System.out.println("TestClass.staticValue:  " + TestClass.staticValue);

        TestClass instance1 = new TestClass();
        TestClass instance2 = new TestClass(123);
        System.out.println("\nPrinting instance value of both instances right after instantiation.");
        System.out.println("instance1.instanceValue:  " + instance1.instanceValue);
        System.out.println("instance2.instanceValue:  " + instance2.instanceValue);
        instance1.instanceValue = 888;
        instance2.instanceValue = 999;
        System.out.println("\nPrinting instance values after editing.");
        System.out.println("instance1.instanceValue:  " + instance1.instanceValue);
        System.out.println("instance2.instanceValue:  " + instance2.instanceValue);

     }

     private static void changeStaticValue()
     {
         TestClass.staticValue = 11111;
     }
}


class TestClass
{
    public static int staticValue;
    public int instanceValue;

    public TestClass()
    {

    }
    public TestClass(int instVal)
    {
        this.instanceValue = instVal;
    }
}

【讨论】:

    【解决方案2】:

    有时您需要确保变量在某个范围内。例如,当你得到RandomObject 和字段probability 时,你需要确保概率在 0 和 1 之间。第二个原因,有时我们希望确保该字段不能从类外更改(以防止错误) .因此我们不能从类外部赋值,所以我们需要使用构造函数(或方法)来完成。第三件事是有时我们使用相同的参数来计算几个字段(例如areaside)。还有很多其他原因,例如防止代码重复、使代码更易于阅读或前面提到的封装。

    【讨论】:

      【解决方案3】:

      有时会更好。您的第 2 行更像是 setter,例如 constructor.setValue(2);

      现在看:

      MyConstructor myConstructor  = new MyConstructor(2,3,5,"This is my cons");
      //or
      myConstructor.int1 = 2;
      myConstructor.int2 = 3;
      myConstructor.int3 = 5;
      myConstructor.string1 = "This is my cons";
      

      一行有四行。

      另外,我们不能访问这样的字段,我们应该使用 getter 和 setter。

      当您像这样调用构造函数时,您只需在创建对象时为字段赋值。使用 setter 时,您可以随时访问字段。

      对不起我的英语。希望你能理解。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-02
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        • 2015-02-24
        • 2012-08-25
        • 1970-01-01
        相关资源
        最近更新 更多