OOP 5. Constructor Overloading(构造函数重载) - TimeOfDay

来源:互联网 发布:js获取数组变成地址 编辑:程序博客网 时间:2024/05/17 01:02

OOP 5. Constructor Overloading - TimeOfDay

Purpose of this lesson:
  • Introduce constructor overloading -- multiple constructors differing in number or types of parameters.
  • Typically this is used to provide optional initialization parameters.
New Java language features:
  • Use of "this(...)" to call one constructor from another.
  • The "this(...)" call must be first line a constructor.
Good Practice:
  • Using "this(...)" is much better than duplicating code.

Overloading constructors

It's common to overload constructors - define multiple constructors which differin number and/or types of parameters. For example,exact hours are common, so an additional constructor could be defined which takes only the hour parameter. You can then set to minutes to a default value.

// File   : oop/timeofday/TimeOfDay1c.java// Purpose: A time-of-day class with constructor, which provides//          a more convenient and conventional way to build objects.//          The constructor is overloaded (more than one version) for//          more convenience.// Author : Fred Swartz, 2005-05-04, Placed in public domain.public class TimeOfDay1c {    public int hour;    public int minute;    //==================================================== constructor    public TimeOfDay1c(int h, int m) {        hour   = h;        minute = m;    }    //==================================================== constructor    public TimeOfDay1c(int h) {        hour   = h;        minute = 0;   // Set minutes to 0.    }}


Calling one constructor from another using "this(...)"

Default parameters. It's very common for a constructor with fewer parameters to call a constructor with more parameters, supplying default values. For this usage, the constructors with fewer parameters will frequently consist of only the "this" call.

"this". Instead of calling the constructor with the class name, use the keywordthis. The compiler matches "this" with a constructor with the appropriate number and types of parameters, and calls it.

Must be first. The "this" call must be the very first line of the constructor.

// File   : oop/timeofday/TimeOfDay1d.java// Purpose: A time-of-day class with overloaded constructors.//          One constructor calls the other using "this".// Author : Fred Swartz, 2007-02-27, Placed in public domain.public class TimeOfDay1d {    public int hour;    public int minute;    //==================================================== constructor    public TimeOfDay1d(int h, int m) {        hour   = h;        minute = m;    }    //==================================================== constructor    public TimeOfDay1d(int h) {        this(h, 0);   // Call other constructor.    }}


Hidden constructor call at the beginning of every constructor

The one essential way in which constructors differ from methods is that the first statement of every constructor is either a call on the constructor for the superclass (usingsuper) or a call to another constructor in the same class (usingthis).

Hidden call. You normally don't see the super call because the compiler automatically generates a call to the parameterless superclass constructor for you, but it's generated at the beginning of every constructor which doesn't explicitly call another constructor.

public TimeOfDay1d(int h, int m) {    hour   = h;    minute = m;}

Is the same as.

public TimeOfDay1d(int h, int m) {    super();   // Call Object constructor.    hour   = h;    minute = m;}

"Copy" Constructors

Another reason to overload constructors is to provide a copy constructor,which builds one object from the values of another similar object. Anther way to do this is for a class to implement theclone() method,but we'll illustrate the copy constructor approach here.

// File   : oop/timeofday/TimeOfDay1d.java// Purpose: A time-of-day class with overloaded constructors.//          Implements a copy constructor.// Author : Fred Swartz, 2007-02-27, Placed in public domain.public class TimeOfDay1e {    public int hour;    public int minute;    //==================================================== constructor    public TimeOfDay1e(int h, int m) {        hour   = h;        minute = m;    }    //==================================================== constructor    public TimeOfDay1e(int h) {        this(h, 0);   // Call other constructor.    }    //==================================================== constructor    public TimeOfDay1e(TimeOfDay1e other) {        this(other.hour, other.minute);   // Call other constructor.    }}


Questions

  1. What would happen if you called the following constructor?
    TimeOfDay1c dentistAppointment = new TimeOfDay1c();

原创粉丝点击