PDA

View Full Version : چند کانستراکتور همراه هم



abbasalim
جمعه 30 فروردین 1392, 22:09 عصر
ب خ
سلام

تو یکی از کانستراکتور ها اومده خوده کلاس رو دوباره گذاشته
من این رو نمیفهمم یعنی چجوری میشه؟ کجا چی چجور متغیر میگیریه میزاره،کلا خیلی این کانستراکتورش گیج کنندس:افسرده:واسم

فهمیدم: میاد کانستراکتورهای اون کلاس رو میزاره داخل این



// Fig. 8.5: Time2.java
// Time2 class declaration with overloaded constructors.

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

// validate and set second
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 Time2

/************************************************** ************************
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
************************************************** ***********************/

cups_of_java
جمعه 30 فروردین 1392, 22:42 عصر
به کانستراکتور هایی که توی ورودی متغیری از نوع خود کلاسشون میگیرن میگن کپی کانستراکتور (Copy Constructor)
که برای کپی کردن یک شی از اون کلاس توی شی جدید استفاده میشه.
فرض کنید شما می خواید یه شی زمان از روی شی زمانی که الان دارید کپی کنید (یعنی 2 تا شی داشته باشید ولی با یک مقادیر) اینجا شما اگه کپی کانستراکتور نوشته باشید میگید:

Time2 t2 = new Time2( t1 );
آگر نداشته باشید مجبورید مثلن بنویسید:

Time2 t2 = new Time2( t1.getHour(), t1.getMinutes(), t1.getSeconds() );
که این خوب سخت تره و می تونه منشا خطا باشه و فراخواننده رو از درگیر جزییات اطلاعاتی که برای ساختن اون شی می خواد میکنه.

spiderman200700
شنبه 31 فروردین 1392, 13:11 عصر
کلمه this دو تا کاربرد داره.
1. استفاده برای اشاره کردن به فیلد های ( همون متغییر ها) سراسری و متد ها در شی فعلی ای که داخلش هستیم.
2. استفاده برای فراخوانی کانسترکتور های دیگه ی برنامه از داخل کانسترکتور دیگه برنامه. که شرط استفاده از این حالت اینه که فراخوانی کانسترکتور دیگه در اولین خط کانسترکتوری که داخلش هستیم انجام بشه.
توی این برنامه هم اومده از پارامتر های یک کانسترکتور استفاده کرده و کانسترکتور دیگه رو صدا کرده.