2011-02-06 77 views
0
// Fig. 9.8: BasePlusCommissionEmployee.java 
// private superclass members cannot be accessed in a subclass. 

public class BasePlusCommissionEmployee extends CommissionEmployee 
{ 
    private double baseSalary; // base salary per week 

    // six-argument constructor 
    public BasePlusCommissionEmployee(String first, String last, 
     String ssn, double sales, double rate, double salary) 
    { 
     // explicit call to superclass CommissionEmployee constructor 
     super(first, last, ssn, sales, rate); 

     setBaseSalary(salary); // validate and store base salary 
    } // end six-argument BasePlusCommissionEmployee constructor 

    // set base salary 
    public void setBaseSalary(double salary) 
    { 
     baseSalary = (salary < 0.0) ? 0.0 : salary; 
    } // end method setBaseSalary 

    // return base salary 
    public double getBaseSalary() 
    { 
     return baseSalary; 
    } // end method getBaseSalary 

    // calculate earnings 
    @Override // indicates that this method overrides a superclass method 
    public double earnings() 
    { 
     // not allowed: commissionRate and grossSales private in superclass 
     return baseSalary + (commissionRate * grossSales); 
    } // end method earnings 

    // return String representation of BasePlusCommissionEmployee 
    @Override // indicates that this method overrides a superclass method 
    public String toString() 
    { 
     // not allowed: attempts to access private superclass members 
     return String.format( 
     "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f", 
     "base-salaried commission employee", firstName, lastName, 
     "social security number", socialSecurityNumber, 
     "gross sales", grossSales, "commission rate", commissionRate, 
     "base salary", baseSalary); 
    } // end method toString 
} // end class BasePlusCommissionEmployee 

// Fig. 9.4: CommissionEmployee.java 
// CommissionEmployee class represents a commission employee. 

public class CommissionEmployee extends Object 
{ 
    private String firstName; 
    private String lastName; 
    private String socialSecurityNumber; 
    private double grossSales; // gross weekly sales 
    private double commissionRate; // commission percentage 

    // five-argument constructor 
    public CommissionEmployee(String first, String last, String ssn, 
     double sales, double rate) 
    { 
     // implicit call to Object constructor occurs here 
     firstName = first; 
     lastName = last; 
     socialSecurityNumber = ssn; 
     setGrossSales(sales); // validate and store gross sales 
     setCommissionRate(rate); // validate and store commission rate 
    } // end five-argument CommissionEmployee constructor 

    // set first name 
    public void setFirstName(String first) 
    { 
     firstName = first; // should validate 
    } // end method setFirstName 

    // return first name 
    public String getFirstName() 
    { 
     return firstName; 
    } // end method getFirstName 

    // set last name 
    public void setLastName(String last) 
    { 
     lastName = last; // should validate 
    } // end method setLastName 

    // return last name 
    public String getLastName() 
    { 
     return lastName; 
    } // end method getLastName 

    // set social security number 
    public void setSocialSecurityNumber(String ssn) 
    { 
     socialSecurityNumber = ssn; // should validate 
    } // end method setSocialSecurityNumber 

    // return social security number 
    public String getSocialSecurityNumber() 
    { 
     return socialSecurityNumber; 
    } // end method getSocialSecurityNumber 

    // set commission employee's gross sales amount 
    public void setGrossSales(double sales) 
    { 
     grossSales = (sales < 0.0) ? 0.0 : sales; 
    } // end method setGrossSales 

    // return commission employee's gross sales amount 
    public double getGrossSales() 
    { 
     return grossSales; 
    } // end method getGrossSales 

    // set commission employee's rate 
    public void setCommissionRate(double rate) 
    { 
     commissionRate = (rate > 0.0 && rate < 1.0) ? rate : 0.0; 
    } // end method setCommissionRate 

    // return commission employee's rate 
    public double getCommissionRate() 
    { 
     return commissionRate; 
    } // end method getCommissionRate 

    // calculate commission employee's pay 
    public double earnings() 
    { 
     return commissionRate * grossSales; 
    } // end method earnings 

    // return String representation of CommissionEmployee object 
    @Override // indicates that this method overrides a superclass method 
    public String toString() 
    { 
     return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 
     "commission employee", firstName, lastName, 
     "social security number", socialSecurityNumber, 
     "gross sales", grossSales, 
     "commission rate", commissionRate); 
    } // end method toString 
} // end class CommissionEmployee 

我迷失在從哪裏開始。我明白,構圖是一種「一種」關係,繼承是一種「是一種」。我是否需要使用這些代碼並使用類中的類重寫它?重寫給定的類使用組合而不是繼承

+0

'//設置第一name`評論這樣的服務沒有有用的目的,只是混亂你的代碼。特別是`//結束方法名稱「部分。有時如果這樣做很有意義,如果它很長,會使嵌套範圍混淆,但適當的縮進通常是更好的方式來說明這一點。 – Falmarri 2011-02-06 19:30:52

回答

1

沒有必要。首先考慮接口或者甚至一些示例代碼。你有一個員工;該員工「擁有」基本工資和佣金。他們兩個都是可能比員工的SSAN更經常變化的事情,所以他們有意義的將自己作爲班級打散。

現在你有三個類(這是故意沒有在Java中的符號):

class Employee 
    ssan : SocialSecurityNumber // more classes I don't bother to define 
    base : BasePay 
    cmsn : Commission 

    public setBasePay(base:BasePay) 
    public getBasePay() 
    public setCommission(cmsn:Commission) 
    public getCommission() 

    ... more stuff 
end 

class BasePay 

    rate: DollarsPerHour 

    public getPay(hr : Hours) 
end 

class Commission 
    rate : Pct 

    public getPay(sales : Dollars) 
end 

和地方,你會碰到這樣的

check.totalPay = employee.getBasePay().getPay(hrs) + // note thats an instance 
       employee.getCommision().getPay(totSales) 

順便說一句,我發現它有用想想這些事情,就好像我爲大多數事情都有特殊的課程,比如DollarsPerHour。最後,您可以替換的東西,如double,但是當你想着它,它有助於保持單位和阻止你做這樣的事情將美元小時