【问题标题】:(Java begineer) "this" keyword discussion [duplicate](Java初学者)“this”关键字讨论[重复]
【发布时间】:2020-05-01 12:56:10
【问题描述】:

我知道“this”关键字的含义。如果我们有一个与实例变量同名的方法参数,我们会使用它来引用实例变量,但是在这段代码中我不明白我们为什么使用它。 “ this( 0, 0, 0 ) ” 甚至是什么意思?我们可以把论点放在教官里面。谁能解释一下?

public class Time2
  {
          private int hour; // 0 - 23
          private int minute; // 0 - 59
          private int second; // 0 - 59

          // Time2 no-argument constructor: initializes each instance variable
          // to zero; ensures that Time2 objects start in a consistent state
          public Time2()
                  {
                  this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 no-argument constructor

          // Time2 constructor: hour supplied, minute and second defaulted to 0
          public Time2( int h )
                  {
                  this ( h, 0, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 one-argument constructor

          // Time2 constructor: hour and minute supplied, second defaulted to 0
          public Time2( int h, int m )
                  {
                  this( h, m, 0 ); // invoke Time2 constructor with three arguments
                  } // end Time2 two-argument constructor

          // Time2 constructor: hour, minute and second supplied
          public Time2( int h, int m, int s )
                  {
                  setTime( h, m, s ); // invoke setTime to validate time
                  } // end Time2 three-argument constructor

          // Time2 constructor: another Time2 object supplied
          public Time2( Time2 time )
                  {
                  // invoke Time2 three-argument constructor
                  this( time.getHour(), time.getMinute(), time.getSecond() );
                  } // end Time2 constructor with a Time2 object argument

          // Set Methods
          // set a new time value using universal time; ensure that
          // the data remains consistent by setting invalid values to zero
          public void setTime( int h, int m, int s )
                  {
                  setHour( h ); // set the hour
                  setMinute( m ); // set the minute
                  setSecond( s ); // set the second
                  } // end method setTime

          // validate and set hour
          public void setHour( int h )
                  {
                  hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
                  } // end method setHour

                  // validate and set minute
                   public void setMinute( int m )
                   {
                   minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
                  } // end method setMinute
                   public void setSecond( int s )
                  {
                   second = ( ( s >= 0 && s < 60 ) ? s : 0 );
                   } // end method setSecond

                  // Get Methods
                   // get hour value
                   public int getHour()
                   {
                   return hour;
                   } // end method getHour

                   // get minute value
                   public int getMinute()
                   {
                   return minute;
                   } // end method getMinute

                   // get second value
                   public int getSecond()
                   {
                   return second;
                   } // end method getSecond

                   // convert to String in universal-time format (HH:MM:SS)
                   public String toUniversalString()
                   {
                   return String.format(
                   "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
                   } // end method toUniversalString

                   // convert to String in standard-time format (H:MM:SS AM or PM)
                   public String toString()
                   {
                   return String.format( "%d:%02d:%02d %s",
                   ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
                   getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
                   } // end method toString
                   } // end class Time2ere

【问题讨论】:

  • this( 0, 0, 0 ) 用 3 个参数调用另一个构造函数
  • 您要查找的搜索词是“构造函数链接”。

标签: java


【解决方案1】:

this( 0, 0, 0 ) 用 3 个参数调用另一个构造函数

所以

public Time2()
{
    this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 no-argument constructor

来电:

public Time2( int h, int m, int s )
{
    setTime( h, m, s ); // invoke setTime to validate time
} // end Time2 three-argument constructor

h,m,s = 0

【讨论】:

  • 作为 OP 的额外信息,这类似于当您调用诸如 super(0,0,0) 之类的东西时,它会使用匹配的参数调用 parent 类的构造函数,您可能以前见过。
【解决方案2】:

您只是从自身内部调用构造函数。

假设我们有一个班级Ball

public class Ball {
  private Color color;
  private int size;

  // This is our constructor
  public Ball(Color c, int s) {
    this.color = c;
    this.size = s;
  }
}

现在,如果我们想创建一个大小为 4 的新红球,我们可以使用 new Ball(Color.RED, 4); 来实现

this 指的是当前对象。因此,如果我们在Ball 内并创建一个新的Ball,我们将使用this(Color.RED, 4);

这实际上就像是在说new Ball();

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2015-02-11
    相关资源
    最近更新 更多