我有一个名为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扩展Loan类,它看起来像:
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所做的是正确的吗? (egthis.client = client)
(b)我可以在方法中拨打超级两次吗?我必须从CarLoan类的构造函数中设置Loan类的两个属性,我认为这是一种方法。
(c)您是否必须在构造函数或getter / setter方法中以不同方式设置枚举类型的属性?我在CarLoan类中得到(this.length = length)错误,我不确定应该如何设置枚举值。谢谢!
答案 0 :(得分:2)
好的,按顺序:
setClient
看起来很好。它没有错。但是,您希望避免直接在this.client
构造函数中设置CarLoan
- 您已经在调用setClient
(感谢@Gabriel和@Aeth)。super
访问父类方法。你需要注意的是调用超类'构造函数,你只能做一次并在子类'构造函数的开头。 super != super()
。this.length = length
没问题。问题是您没有名为length
的字段。您可能想要添加其中一个。答案 1 :(得分:1)
1)在构造函数和方法之前放置类的属性的声明是常规的。
2)this.client = client;
类中的语句CarLoan
将给出编译错误,因为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
代表货币价值将是一个很大的禁忌!
答案 2 :(得分:0)
回答问题(c),我认为你得到了错误,因为你需要在那里定义长度以及你已经定义的变量。