OOP 5. Constructor Overloading(构造函数重载) - TimeOfDay
来源:互联网 发布:js获取数组变成地址 编辑:程序博客网 时间:2024/05/17 01:02
OOP 5. Constructor Overloading - TimeOfDay
- Introduce constructor overloading -- multiple constructors differing in number or types of parameters.
- Typically this is used to provide optional initialization parameters.
- Use of "this(...)" to call one constructor from another.
- The "this(...)" call must be first line a constructor.
- 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
- What would happen if you called the following constructor?
TimeOfDay1c dentistAppointment = new TimeOfDay1c();
- OOP 5. Constructor Overloading(构造函数重载) - TimeOfDay
- [C++] Call an overloading constructor within a constructor (C++上构造函数里调用重载构造函数)
- CRITICAL SKILL 9.1: Overloading Constructors 构造函数的重载
- C++中的函数重载(overloading)
- Java--构造方法(Constructor)、重载
- 构造函数(constructor)
- 构造函数CONSTRUCTOR
- 构造函数constructor
- 动态函数重载(Dynamic Function Overloading)
- 动态函数重载(Dynamic Function Overloading)
- 函数重载(overloading)应注意的问题
- 复制构造函数(copy constructor)
- default constructor(默认构造函数)
- default constructor 的构造函数
- Copy constructor拷贝构造函数
- explicit constructor(显示构造函数)
- 拷贝构造函数 (Copy constructor)
- initCompent()和构造函数(constructor)
- smarty内建函数
- 有关loadrunner英文面试题目,转过来的~
- linux常用命令
- 8种Nosql数据库系统对比
- Javascript 四舍五入
- OOP 5. Constructor Overloading(构造函数重载) - TimeOfDay
- OpenVPN高级路由技术-反向推送信息
- lotus domino/notes权限控制文档
- 集合初始化需要注意的一个问题
- Ubuntu 编译android 源码
- C++进阶之路
- LINUX设备驱动——输入子系统
- 求交集、并集和差集
- 继续聊WPF——为ListView的行设置样式