时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 重载构造函数的类声明
Java 重载构造函数的类声明
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
433
查看 / 运行 实例源码
重载构造函数的类声明
实例源码:
源代码:
执行
执行中...
public class Main { public static void main(String[] args) /** from * 时代Java公众号 - N o w J a v a . c o m **/ { Time2 t1 = new Time2(); // 00:00:00 Time2 t2 = new Time2(2); // 02:00:00 Time2 t3 = new Time2(21, 31); // 21:31:00 Time2 t4 = new Time2(12, 25, 42); // 12:25:42 Time2 t5 = new Time2(t4); // 12:25:42 System.out.println("Constructed with:"); displayTime("t1: all default arguments", t1); displayTime("t2: hour specified; default minute and second", t2); displayTime("t3: hour and minute specified; default second", t3); displayTime("t4: hour, minute and second specified", t4); displayTime("t5: Time2 object t4 specified", t5);/**来自 n o w j a v a . c o m**/ // attempt to initialize t6 with invalid values try { Time2 t6 = new Time2(27, 74, 99); // invalid values } catch (IllegalArgumentException e) { System.out.printf("%nException while initializing t6: %s%n", e.getMessage()); } } // displays a Time2 object in 24-hour and 12-hour formats private static void displayTime(String header, Time2 t) { System.out.printf("%s%n %s%n %s%n", header, t.toUniversalString(), t.toString()); } } 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 public Time2() { this(0, 0, 0); // invoke constructor with three arguments } // Time2 constructor: hour supplied, minute and second defaulted to 0 public Time2(int hour) { this(hour, 0, 0); // invoke constructor with three arguments } // Time2 constructor: hour and minute supplied, second defaulted to 0 public Time2(int hour, int minute) { this(hour, minute, 0); // invoke constructor with three arguments } // Time2 constructor: hour, minute and second supplied public Time2(int hour, int minute, int second) { if (hour < 0 || hour >= 24) throw new IllegalArgumentException("hour must be 0-23"); if (minute < 0 || minute >= 60) throw new IllegalArgumentException("minute must be 0-59"); if (second < 0 || second >= 60) throw new IllegalArgumentException("second must be 0-59"); this.hour = hour; this.minute = minute; this.second = second; } // Time2 constructor: another Time2 object supplied public Time2(Time2 time) { // invoke constructor with three arguments this(time.getHour(), time.getMinute(), time.getSecond()); } public void setTime(int hour, int minute, int second) { if (hour < 0 || hour >= 24) throw new IllegalArgumentException("hour must be 0-23"); if (minute < 0 || minute >= 60) throw new IllegalArgumentException("minute must be 0-59"); if (second < 0 || second >= 60) throw new IllegalArgumentException("second must be 0-59"); this.hour = hour; this.minute = minute; this.second = second; } // validate and set hour public void setHour(int hour) { if (hour < 0 || hour >= 24) throw new IllegalArgumentException("hour must be 0-23"); this.hour = hour; } // validate and set minute public void setMinute(int minute) { if (minute < 0 || minute >= 60) throw new IllegalArgumentException("minute must be 0-59"); this.minute = minute; } // validate and set second public void setSecond(int second) { if (second < 0 || second >= 60) throw new IllegalArgumentException("second must be 0-59"); this.second = second; } // Get Methods // get hour value public int getHour() { /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
Constructed with:
t1: all default arguments
00:00:00
12:00:00 AM
t2: hour specified; default minute and second
02:00:00
2:00:00 AM
t3: hour and minute specified; default second
21:31:00
9:31:00 PM
t4: hour, minute and second specified
12:25:42
12:25:42 PM
t5: Time2 object t4 specified
12:25:42
12:25:42 PM
Exception while initializing t6: hour must be 0-23
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部