package com.example.java;
public class Time2 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
private int millisecond; // 0 - 999
public Time2() {
this(0, 0, 0, 0);
}
public Time2(int hour) {
this(hour, 0, 0, 0);
}
public Time2(int hour,int minute) {
this(hour, minute, 0, 0);
}
public Time2(int hour,int minute, int second) {
this(hour, minute, second, 0);
}
public Time2(int hour, int minute, int second,int millisecond) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("輸入的 小時 必須在 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("輸入的 分 必須在 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("輸入的 秒 必須在 0-59");
if (millisecond < 0 || millisecond >= 999)
throw new IllegalArgumentException("輸入的 微秒 必須在 0-999");
this.hour = hour;
this.minute = minute;
this.second = second;
this.millisecond = millisecond;
}
public Time2(Time2 t) {
this(t.getHour(), t.getMinute(), t.getSecond(), t.getMillisecond());
}
public void setHour(int hour) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("輸入的 小時 必須在 0-23");
this.hour = hour;
}
public void setMinute(int minute) {
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("輸入的 分 必須在 0-59");
this.minute = minute;
}
public void setSecond(int second) {
if (second < 0 || second >= 60)
throw new IllegalArgumentException("輸入的 秒 必須在 0-59");
this.second = second;
}
public void setMillisecond(int millisecond) {
if (millisecond < 0 || millisecond >= 999)
throw new IllegalArgumentException("輸入的 微秒 必須在 0-999");
this.millisecond = millisecond;
}
public int getHour() {
return this.hour;
}
public int getMinute() {
return this.minute;
}
public int getSecond() {
return this.second;
}
public int getMillisecond() {
return this.millisecond;
}
public String toUniversalString() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
public String toString() {
return String.format("%d:%02d:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12), minute, second,
(hour < 12 ? "AM" : "PM"));
}
}
###自建類別或物件
其實就是
宣告一些變數來放值
New些物件來用
多載些不同參數型態數量的類別或物件
提供些方法讓人用
最後再加上繼承、多型
接著老師在講
物件模組化
低耦合高內聚力
減少行數的
低維護成本
降低錯誤可能性
概念
##補充概念:
Set 又稱改變方法 mutator method
Get 又稱讀取方法accessor method
看ptt補
實作get set好像和
把實體變數宣告為public沒啥兩樣
1. 因為宣告為public他會沒辦法驗證參數
2. 具有隱私性 ( 商業邏輯可以藏在裡面
雖然get set 方法提供了存取 private 資料的途徑,但其實還是在現制的方法下
Set方法具有有效性的檢查
但並不會宣告private就自動出現,必須自己實作
內建的
判定方法predicate method
例如 ArrrayList的isEmpty方法
###接著 Time2Test.java
實作try呼叫剛剛做的函數 crath錯誤訊息物件
###程式碼
package com.example.java;
public class Time2Test {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub.
Time2 t1, t2, t3, t4, t5, t6;
t1 = new Time2();
t2 = new Time2(2);
t3 = new Time2(21, 35);
t4 = new Time2(21, 35, 46);
t5 = new Time2(21, 35, 24, 55);
try {
t6 = new Time2(27, 35, 24, 55);
} catch (IllegalArgumentException ex) {
System.out.printf("錯誤發生! 物件初始畫錯誤 : %s %n%n", ex.getMessage());
}
displayTime("t1:時、分、秒、毫秒初始化為預設值 ", t1);
displayTime("t2:指定小時,分、秒、毫秒初始化為預設值 ", t2);
displayTime("t3:指定小時、分,秒、毫秒初始化為預設值 ", t3);
displayTime("t4:指定小時、分、秒,毫秒初始化為預設值 ", t4);
displayTime("t5:指定小時、分、秒、毫秒,初始化", t5);
}
private static void displayTime(String header, Time2 t) {
System.out.printf("%s%n %s%n %s%n", header, t.toUniversalString(), t.toString());
}
}
##複合 composition
類別可以包含指向其他類別物件的參照(參數),做為類別成員
擁有關係(has-arelationship
###案例Employee自訂類別表示特定員工身分 包含其他類別
###檔案Date.java類別 ( 老師之前實作了
需要紀錄時間所以指向Date物件(生日與雇用時間)
String其實也是類別型態的變數
###老師程式碼
package com.example.java;
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
private static final int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public Date(int month, int day) {
this(month, day, 2019);
}
public Date(int month, int day, int year) {
if (month <= 0 || month > 12)
throw new IllegalArgumentException("月份錯誤 (" + month + ") 必須在 1-12");
if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("日期錯誤 (" + day + ") 超過指定年、月的正常範圍");
if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
throw new IllegalArgumentException("日期錯誤 (" + day + ") 超過指定年、月的正常範圍");
this.month = month;
this.day = day;
this.year = year;
System.out.printf("Date 物件初始化成功: %s%n", this);
}
public String toString() {
return String.format("%d/%d/%d", month, day, year);
}
}
接著實作
###Employee.java
展示複合
package com.example.java;
public class Employee {
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;
public Employee(String firstName, String lastName, Date birthDate, Date hireDate) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
}
public String toString() {
return String.format("%s,%s-雇用日期: %s-出生日期: %s", lastName, firstName, hireDate, birthDate);
}
}
###EmployeeTest.java 展示複合
package com.example.java;
public class EmployeeTest {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Date birth = new Date(7, 24, 2005);
Date hire = new Date(3, 12, 2016);
Employee employee = new Employee("連杰", "李", birth, hire);
System.out.println(employee);
}
}