Home  >  Q&A  >  body text

java - 既然有参构造方法中,参数可以通过new 赋值,为什么还要通过参数赋值给未初始化的成员变量来输出结果?

PHP中文网PHP中文网2741 days ago548

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:52:06

    You are probably talking about assigning values ​​directly when writing fields. This is similar to assigning a value in the constructor. However, writing it directly on the field is generally regarded as the default value. If you need to pass in different values ​​​​according to the situation, you need a constructor.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:52:06

    If you have multiple constructors, and some constructors do not initialize some member variables, then the remaining member variables do not have default values ​​set, something may go wrong

    reply
    0
  • Cancelreply