我有一个名为Person的类,基本上长这样:
public class Person
{
String firstName;
String lastName;
String telephone;
String email;
public Person()
{
firstName = "";
lastName = "";
telephone = "";
email = "";
}
public Person(String firstName, String lastName, String telephone, String email)
{
this.firstName = firstName;
this.lastName = lastName;
this.telephone = telephone;
this.email = email;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
....
使用那个类,我设置了一个抽象类称为Loan,它的外观如下:
public abstract class Loan
{
public void setClient(Person client)
{
this.client = client;
}
public Person getClient()
{
return client;
}
public void setLoanId(int nextId)
{
loanId = nextId;
nextId++;
}
public int getLoanId()
{
return loanId;
}
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
}
public double getInterestRate()
{
return interestRate;
}
public void setLoanLength(int loanLength)
{
this.loanLength = loanLength;
}
public int getLoanLength()
{
return loanLength;
}
public void setLoanAmount(double loanAmount)
{
this.loanAmount = loanAmount;
}
public double getLoanAmount(double loanAmount)
{
return loanAmount;
}
private Person client;
private int loanId;
private double interestRate;
private int loanLength;
private double loanAmount;
private static int nextId = 1;
}
我需要扩展Loan类,创建一个CarLoan类,代码如下:
public class CarLoan extends Loan
{
public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax,
double interestRate, CAR_LOAN_TERMS length)
{
super.setClient(client);
super.setInterestRate(interestRate);
this.client = client;
this.vehiclePrice = vehiclePrice;
this.downPayment = downPayment;
this.salesTax = salesTax;
this.length = length;
}
public void setVehiclePrice(double vehiclePrice)
{
this.vehiclePrice = vehiclePrice;
}
public double getVehiclePrice()
{
return vehiclePrice;
}
public void setDownPayment(double downPayment)
{
this.downPayment = downPayment;
}
public double getDownPayment()
{
return downPayment;
}
public void setSalesTax(double salesTax)
{
this.salesTax = salesTax;
}
public double getSalesTax()
{
return salesTax;
}
public String toString()
{
return getClass().getName() + "[vehiclePrice = " + vehiclePrice + '\n'
+ "downPayment = " + downPayment + '\n'
+ "salesTax = " + salesTax
+ "]";
}
public enum CAR_LOAN_TERMS {TWO_YEAR, THREE_YEAR, SIX_YEAR};
private double vehiclePrice;
private double downPayment;
private double salesTax;
几个问题。
(a) 根据我在Person类中的设置,我在Loan类中所做的设置客户的操作是正确的吗?(例如this.client = client)
(b) 我可以在一个方法中两次调用super吗?我必须从CarLoan类的构造函数中设置Loan类中的两个属性,并且我认为这是一种方法。
(c) 在构造函数或getter/setter方法中,是否必须以不同方式设置枚举类型的属性?在我的CarLoan类中,(this.length = length)会出现错误,我不确定枚举值应该如何设置。谢谢!