2019/0521/抽象類別+介面
Last updated
Last updated
函式名稱與()就直接分號了 不會提供{}內實作
類別 繼承 多型 抽象類別
在架構上概念上擴展 員工這個案例 employee
接著實作整個架構的內容
package abstractClass;
public abstract class Employee extends Object{
private final String firstName;
private final String lastName;
private final String idNumber;
public Employee(String firstName, String lastName, String idNumber)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getIdNumber()
{
return idNumber;
}
public abstract double earnings();
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n" ,
"員工姓名", getFirstName(), getLastName(),
"身分證字號", getIdNumber());
}
}
package abstractClass;
public class CommissionEmployee extends Employee{
private double grossSales;
private double commissionRate; // 佣金,抽庸的比例
public CommissionEmployee(String firstName, String lastName, String idNumber, double grossSales, double commissionRate)
{
super(firstName,lastName,idNumber);
if (grossSales < 0.0)
throw new IllegalArgumentException("銷售金額必須 >= 0.0");
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("佣金比例必須 > 0.0 and < 1.0");
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0)
throw new IllegalArgumentException("銷售金額必須 >= 0.0");
this.grossSales = grossSales;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("佣金比例必須 > 0.0 and < 1.0");
this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
return commissionRate;
}
@Override
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
return String.format("%s%n%s: %.2f%n%s: %.2f %n%s: %.2f" ,
super.toString(),
"銷售金額", getGrossSales(),
"佣金比例", getCommissionRate(),
"總佣金",getCommissionRate() * getGrossSales());
}
}
package abstractClass;
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary; // 底薪
public BasePlusCommissionEmployee(String firstName, String lastName, String idNumber, double grossSales,double commissionRate, double baseSalary) {
super(firstName, lastName, idNumber, grossSales, commissionRate);
if (baseSalary < 0.0)
throw new IllegalArgumentException("底薪金額必須 >= 0.0");
this.baseSalary = baseSalary;
}
public void setBaseSalary(double baseSalary) {
if (baseSalary < 0.0)
throw new IllegalArgumentException("底薪金額必須 >= 0.0");
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
@Override
public double earnings() {
return getBaseSalary() + (super.getCommissionRate() * super.getGrossSales());
}
@Override
public String toString() {
return String.format("%s %n %s: %.2f%n %s: %.2f" ,
super.toString(),
"底薪金額", getBaseSalary(),
"底薪+獎金 金額", this.earnings());
}
}
package abstractClass;
public class WeekSalaryPlusCommissionEmployee extends CommissionEmployee {
private double weekSalary; // 周薪
public WeekSalaryPlusCommissionEmployee(String firstName, String lastName, String idNumber, double grossSales,double commissionRate, double weekSalary) {
super(firstName, lastName, idNumber, grossSales, commissionRate);
if (weekSalary < 0.0)
throw new IllegalArgumentException("周薪金額必須 >= 0.0");
this.weekSalary = weekSalary;
}
public void setWeekSalary(double weekSalary) {
if (weekSalary < 0.0)
throw new IllegalArgumentException("周薪金額必須 >= 0.0");
this.weekSalary = weekSalary;
}
public double getWeekSalary() {
return weekSalary;
}
@Override
public double earnings() {
return getWeekSalary() + (super.getCommissionRate() * super.getGrossSales());
}
@Override
public String toString() {
return String.format("%s%n %s %n%s: %.2f%n %s: %.2f" , "領周薪加獎金的員工",
super.toString(), "周薪金額", getWeekSalary(),"周薪+獎金 金額", this.earnings() );
}
}
package abstractClass;
public class PaySystemTest {
public static void main(String[] args) {
BasePlusCommissionEmployee employee01 = new BasePlusCommissionEmployee("劉", "德華", "A123456789", 10000, .06,1000);
CommissionEmployee employee02 = new CommissionEmployee("陳", "立農", "A123456789", 50000, .1);
WeekSalaryPlusCommissionEmployee employee03 = new WeekSalaryPlusCommissionEmployee ("韓", "國魚", "A333333333", 2000, .05, 500);
System.out.printf("%n%s:%n%n%s%n", "第 1 位員工薪資資料", employee01.toString());
System.out.printf("%n%s:%n%n%s%n", "第 2 位員工薪資資料", employee02.toString());
System.out.printf("%n%s:%n%n%s%n", "第 3 位員工薪資資料", employee03.toString());
}
}
到這裡還沒有多型的應用只是類別繼承抽象類別,等等會講應用
剛剛是抽傭的員工(commission employee)
接著要做 時薪員工 ( hourly employee )
package abstractClass;
public class HourlyEmployee extends Employee {
private double wage;
private double hours; // 工時
public HourlyEmployee(String firstName, String lastName, String idNumber, double wage, double hours) {
super(firstName, lastName, idNumber);
if (wage < 0.0)
throw new IllegalArgumentException("時薪必須 >= 0.0");
if (hours <= 0.0 || hours >= 720.0)
throw new IllegalArgumentException("工作時數必須 > 0.0 and < 720.0");
this.wage = wage;
this.hours = hours;
}
public void setWage(double wage) {
if (wage < 0.0)
throw new IllegalArgumentException("時薪必須 >= 0.0");
this.wage = wage;
}
public double getWage() {
return wage;
}
public void setHours(double hours) {
if (hours <= 0.0 || hours >= 720.0)
throw new IllegalArgumentException("工作時數必須 > 0.0 and < 720.0");
this.hours = hours;
}
public double getHours() {
return hours;
}
@Override
public double earnings() {
if (getHours() <= 40)
return getHours() * getWage();
else
return (40 * getWage()) + ((getHours() - 40) * (getWage() * 1.5));
}
@Override
public String toString() {
return String.format("%s%n%s: %.2f%n%s: %.2f %n%s: %.2f", super.toString(), "單位時薪", getWage(), "工作時數",
getHours(), "總時薪", this.earnings());
}
}
接著做Salaried employee 固定薪資員工
package abstractClass;
public class SalariedEmployee extends Employee {
private double salary; //薪資
public SalariedEmployee(String firstName, String lastName, String idNumber, double salary) {
super(firstName, lastName, idNumber);
if (salary < 0.0)
throw new IllegalArgumentException("薪資必須 >= 0.0");
this.salary = salary;
}
public void setSalary(double salary) {
if (salary < 0.0)
throw new IllegalArgumentException("薪資必須 >= 0.0");
this.salary = salary;
}
public double getSalary() {
return salary;
}
@Override
public double earnings() {
return getSalary();
}
@Override
public String toString() {
return String.format("%s%n%s: %.2f", super.toString(), "固定薪資", this.earnings());
}
}
針對底薪加獎金的員工做加薪動作
做再
package abstractClass;
public class PaySystemTest {
public static void main(String[] args) {
BasePlusCommissionEmployee employee01 = new BasePlusCommissionEmployee("劉", "德華", "A123456789", 10000, .06,1000);
CommissionEmployee employee02 = new CommissionEmployee("陳", "立農", "A123456789", 50000, .1);
WeekSalaryPlusCommissionEmployee employee03 = new WeekSalaryPlusCommissionEmployee("韓", "國魚", "A333333333", 2000, .05, 500);
HourlyEmployee employee04 = new HourlyEmployee("方", "大同", "A1111111111", 150, 60);
// System.out.printf("%n%s:%n%n%s%n", "第 1 位員工薪資資料", employee02.toString());
// System.out.printf("%n%s:%n%n%s%n", "第 2 位員工薪資資料", employee01.toString());
// System.out.printf("%n%s:%n%n%s%n", "第 3 位員工薪資資料", employee03.toString());
// System.out.printf("%n%s:%n%n%s%n", "第 4 位員工薪資資料", employee04.toString());
Employee empArray[] = new Employee[7];
empArray[0] = employee01;
empArray[1] = employee02;
empArray[2] = employee03;
empArray[3] = employee04;
empArray[4] = new SalariedEmployee("周", "潤發", "A2222222222", 55000.0);
empArray[5] = new BasePlusCommissionEmployee("李", "連杰", "A444444444", 20000, .06,2000);
empArray[6] = new BasePlusCommissionEmployee("郭", "富城", "A555555555", 10000, .06,3000);
for(int i = 0; i < empArray.length ;i++)
{
System.out.printf("%n%s%d%s:%n%n%s%n", "第 ",i+1," 位員工薪資資料", empArray[i].toString());
}
for(int i = 0,j=0; i < empArray.length ;i++)
{
if (empArray[i] instanceof BasePlusCommissionEmployee)
{
((BasePlusCommissionEmployee)empArray[i]).setBaseSalary(1.1 * ((BasePlusCommissionEmployee)empArray[i]).getBaseSalary());
System.out.printf("%n%s%d%s:%n%n%s%n", "第 ",j+1," 位領底薪+獎金員工,加薪10%後薪資資料", empArray[i].toString());
j++;
}
}
}
}
剛剛幾種類別之間都有繼承關係 員工 ( 各種不同的員工
子類別必須要是一種父類別
不管是領時薪或領月薪的員工都是員工
之前階段?有接觸
都是算錢 但是無繼承關係 所以就要使用介面
彼此之間有關係 就用抽象類別 做繼承
但類別之間沒有繼承關係 ( 進貨單 並不是一種員工
這種時候就要使用 interface 介面
抽象方法 定義有繼承關係的類別
介面 定義無繼承關係的類別
記得介面裡面不可以有任何實體變數與方法
抽象類別可以實作但沒時做的就必須繼續抽像宣告下去
Employee.java 內
getPaymentAmount() 要取代原本的earnings()
但在這個地方還是
沒辦法實作 getPaymentAmount()
所以只要 implements Payable{}
再將earnings() 刪除即可
即可
接著實作 CommissionEmployee.java
只要將earnings() 改為 getPaymentAmount() 即可
做個InterfaceTest.java來測試
介面操作多型期時跟抽象類別操作多型類似
指是乘載的父輩變介面類別
記得介面裡面不可以有任何實體變數與方法
做完了Payable 與 employee 與 salaried employee
接著
實作 Invoice
材料編號 材料敘述 單價 數量
final 說明
package interfaceClass;
public interface Payable {
public abstract double getPaymentAmount();
}
package interfaceClass;
public abstract class Employee extends Object implements Payable{
private final String firstName;
private final String lastName;
private final String idNumber;
public Employee(String firstName, String lastName, String idNumber)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getIdNumber()
{
return idNumber;
}
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n" ,
"員工姓名", getFirstName(), getLastName(),
"身分證字號", getIdNumber());
}
}
package interfaceClass;
public class CommissionEmployee extends Employee{
private double grossSales;
private double commissionRate; // 佣金,抽庸的比例
public CommissionEmployee(String firstName, String lastName, String idNumber, double grossSales, double commissionRate)
{
super(firstName,lastName,idNumber);
if (grossSales < 0.0)
throw new IllegalArgumentException("銷售金額必須 >= 0.0");
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("佣金比例必須 > 0.0 and < 1.0");
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0)
throw new IllegalArgumentException("銷售金額必須 >= 0.0");
this.grossSales = grossSales;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("佣金比例必須 > 0.0 and < 1.0");
this.commissionRate = commissionRate;
}
public double getCommissionRate()
{
return commissionRate;
}
public double getPaymentAmount()
{
return getCommissionRate() * getGrossSales();
}
@Override
public String toString()
{
return String.format("%s%n%s: %.2f%n%s: %.2f %n%s: %.2f" ,
super.toString(),
"銷售金額", getGrossSales(),
"佣金比例", getCommissionRate(),
"總佣金",getCommissionRate() * getGrossSales());
}
}
package interfaceClass;
public class SalariedEmployee extends Employee {
private double salary; //薪資
public SalariedEmployee(String firstName, String lastName, String idNumber, double salary) {
super(firstName, lastName, idNumber);
if (salary < 0.0)
throw new IllegalArgumentException("薪資必須 >= 0.0");
this.salary = salary;
}
public void setSalary(double salary) {
if (salary < 0.0)
throw new IllegalArgumentException("薪資必須 >= 0.0");
this.salary = salary;
}
public double getSalary() {
return salary;
}
@Override
public double getPaymentAmount() {
return getSalary();
}
@Override
public String toString() {
return String.format("%s%n%s: %.2f", super.toString(), "固定薪資", this.getPaymentAmount());
}
}
package interfaceClass;
public class WeekSalaryPlusCommissionEmployee extends CommissionEmployee {
private double weekSalary; // 周薪
public WeekSalaryPlusCommissionEmployee(String firstName, String lastName, String idNumber, double grossSales,double commissionRate, double weekSalary) {
super(firstName, lastName, idNumber, grossSales, commissionRate);
if (weekSalary < 0.0)
throw new IllegalArgumentException("周薪金額必須 >= 0.0");
this.weekSalary = weekSalary;
}
public void setWeekSalary(double weekSalary) {
if (weekSalary < 0.0)
throw new IllegalArgumentException("周薪金額必須 >= 0.0");
this.weekSalary = weekSalary;
}
public double getWeekSalary() {
return weekSalary;
}
@Override
public double getPaymentAmount() {
return getWeekSalary() + (super.getCommissionRate() * super.getGrossSales());
}
@Override
public String toString() {
return String.format("%s%n %s %n%s: %.2f%n %s: %.2f" , "領周薪加獎金的員工",
super.toString(), "周薪金額", getWeekSalary(),
"周薪+獎金 金額", this.getPaymentAmount());
}
}
package interfaceClass;
public class Invoice implements Payable {
private final String partNumber; // 材料編號
private final String partDescription; // 材料敘述
private double pricePerItem; // 單價
private int quantity; // 數量
public Invoice(String partNumber, String partDescription, int quantity, double pricePerItem) {
super();
if (quantity < 0.0)
throw new IllegalArgumentException("貨務數量必須 >= 0.0");
if (pricePerItem < 0.0)
throw new IllegalArgumentException("貨物單價必須 >= 0.0");
this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = quantity;
this.pricePerItem = pricePerItem;
}
public String getPartNumber() {
return partNumber;
}
public String getPartDescription() {
return partDescription;
}
public void setQuantity(int quantity) {
if (quantity < 0.0)
throw new IllegalArgumentException("貨務數量必須 >= 0.0");
this.quantity = quantity;
}
public double getQuantity() {
return quantity;
}
public void setPricePerItem(double pricePerItem) {
if (pricePerItem < 0.0)
throw new IllegalArgumentException("貨物單價必須 >= 0.0");
this.pricePerItem = pricePerItem;
}
public double getPricePerItem() {
return pricePerItem;
}
@Override
public double getPaymentAmount() {
return getQuantity() * getPricePerItem();
}
@Override
public String toString()
{
return String.format("%s: %s%n%s: %s%n%s: %.2f%n%s: %.2f %n%s: %.2f" ,
"貨物編號", getPartNumber(),
"貨物描述", getPartDescription(),
"數量", getQuantity(),
"單價", getPricePerItem(),
"總金額",this.getPaymentAmount());
}
}
package interfaceClass;
public class InterfaceTest {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
// Employee empArray[] = new Employee[7];
Payable payableInterfaceArray[] = new Payable[4];
payableInterfaceArray[0] = new CommissionEmployee("陳", "立農", "A123456789", 50000, .1);
payableInterfaceArray[1] = new SalariedEmployee("周", "潤發", "A2222222222", 55000.0);
payableInterfaceArray[2] = new WeekSalaryPlusCommissionEmployee("韓", "國魚", "A333333333", 2000, .05, 500);
payableInterfaceArray[3] = new Invoice("P0001", "雨衣", 3, 100.0);
for(int i = 0; i < payableInterfaceArray.length ;i++)
{
System.out.printf("%n%s%d%s:%n%n%s%n", "第 ",i+1," 筆,應付款資料", payableInterfaceArray[i].toString());
}
}
}