我有一个名为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;
}我必须用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类中对setClient所做的操作是否正确?(e.g.this.client =客户端)
(b)我可以在一个方法中调用super两次吗?我必须从CarLoan类的构造函数中设置Loan类的两个属性,我认为这是一种方法。
(c)是否必须在构造函数或getter/setter方法中为枚举类型设置不同的属性?我在CarLoan类中得到了一个错误(this.length = length),并且我不确定应该如何设置枚举值。谢谢!
发布于 2010-04-26 08:47:52
1)通常将类的属性声明放在构造函数和方法之前。
2) CarLoan类中的语句this.client = client;会给您一个编译错误,因为client字段在Loan类中声明为私有。(而且该语句无论如何都是多余的,因为您只是使用setter初始化了相同的字段...尽管你已经知道了这一点。)
3)初始化超类字段的更好方法是将参数传递给超类构造函数。例如:
public abstract class Loan
{
private Person client;
private double interestRate;
public Loan(Person client, double interestRate) {
this.client = client;
this.interestRate = interestRate;
}
...
}
public class CarLoan extends Loan
{
...
public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax,
double interestRate, CAR_LOAN_TERMS length)
{
super(client, interestRate);
this.vehiclePrice = vehiclePrice;
...
}
}这种方法更好的原因是Loan类负责它的初始化,而不依赖于各种子类构造函数来完成这项工作。(如果向Loan添加一个额外的字段,并向Loan构造函数添加相应的参数,编译器会提醒您修改所有子类构造函数,以便在super构造函数链中提供初始值。如果子类负责在初始化期间设置基类中的字段,那么编译器不会注意到您忘记添加新的setter调用。)
4)如果在构造函数中调用方法,最好确保它们不会在子类中被重写。(不..。方法被覆盖并不是完全错误的,但有些事情可能会出错。在构造函数中调用可能可重写的方法会使代码变得脆弱。)
5)如果这是生产代码,那么使用float或double来表示货币值将是一个大禁忌!
https://stackoverflow.com/questions/2710260
复制相似问题