Getter/Setter(组合、Java、HW)
我有一个名为 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) 我可以在一个方法中调用 super 两次吗?我必须从 CarLoan 类的构造函数中设置 Loan 类的两个属性,我认为这将是一种方法。
(c) 是否必须在构造函数或 getter/setter 方法中以不同方式设置枚举类型的属性?我在 CarLoan 类中收到 (this.length = length) 错误,并且我不确定应如何设置枚举值。谢谢!
I have one class called Person that basically looks like:
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;
}
....
Using that class, I setup an abstract class called Loan that looks like:
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;
}
I have to extend the Loan class with CarLoan and it looks like:
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;
Few questions.
(a) Is what I did in the Loan class to setClient correct given what I have in the Person class? (e.g.this.client = client)
(b) Can I call super twice in a method? I have to set two attributes from the Loan class from the constructor in the CarLoan class and I thought that would be a way to do it.
(c) Do you have to set attributes for enumeration types differently in a constructor or getter/setter methods? I get an error for (this.length = length) in my CarLoan class and I was unsure of how enumeration values should be set. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,按顺序:
setClient
看起来非常好。没有什么问题。但是,您希望避免直接在CarLoan
构造函数中设置this.client
- 您已经在调用setClient
(感谢 @Gabriel 和 @埃斯)。super
访问父类方法。您需要小心的是调用超类的构造函数,您只能在子类构造函数的开头执行一次。超级!=超级()
。this.length = length
没问题。问题是您没有名为length
的字段。您可能想添加其中之一。OK, in order:
setClient
Looks perfectly fine. Nothing wrong with it. However, you want to avoid settingthis.client
directly in theCarLoan
constructor—you're already callingsetClient
(thanks to @Gabriel and @Aeth).super
to access the parent class methods as much as you want. What you need to be careful with is calling the superclass' constructor, which you can only do once and at the beginning of the subclass' constructor.super != super()
.this.length = length
is fine. The problem is that you don't have a field calledlength
. You might want to add one of those.1) 按照惯例,将类的属性声明放在构造函数和方法之前。
2)
CarLoan
类中的语句this.client = client;
会出现编译错误,因为client
字段在Loan
类。 (无论如何,该语句是多余的,因为您只是使用 setter 初始化了相同的字段......尽管希望您已经知道这一点。)3)初始化超类字段的更好方法是将参数传递给超类构造函数。例如:
这种方法更好的原因是 Loan 类负责其初始化,并且不依赖于各种子类构造函数来完成这项工作。 (如果您向 Loan 添加额外的字段,并将相应的参数添加到 Loan 构造函数中,编译器会提醒您修改所有子类构造函数以提供初始值
super
构造函数链接。如果子类负责在初始化期间设置基类中的字段,则编译器不会注意到您忘记添加新的 setter 调用。)4) 如果您这样做在构造函数中调用方法时,最好确保它们不能在子类中被重写。 (不……重写方法并非完全错误,但有些事情可能会出现严重错误。在构造函数中调用可能可重写的方法会使您的代码变得脆弱。)
5) 如果这是生产代码,请使用
float
或double
来表示货币值将是一个很大的禁忌!1) It is conventional to put the declarations of the attributes of a class before the constructors and methods.
2) The statement
this.client = client;
in theCarLoan
class will give you a compilation error because theclient
field is declared as private in theLoan
class. (And that statement is redundant anyway because you just initialized the same field using the setter ... though expect you already knew that.)3) A better way to initialize the fields of a superclass is to pass the arguments to the superclass constructor. For example:
The reason this approach is better is that the
Loan
class takes responsibility for its initialization, and doesn't rely on the various subclass constructors doing the job. (If you add an extra field toLoan
and add the corresponding parameter to theLoan
constructor, the compiler reminds you to modify all of the subclass constructors to provide the initial value in thesuper
constructor chaining. If the subclasses are responsible for setting fields in the base class during initialization, then the compiler won't notice that you forgot to add the new setter call.)4) If you do call methods in a constructor, it is good practice to ensure that they cannot be overridden in a subclass. (No ... it is not entirely wrong for the methods to be overridden, but there are things that can go horribly wrong. Calling potentially overridable methods in a constructor makes your code fragile.)
5) If this was production code, use of
float
ordouble
to represent currency values would be a big no-no!回答问题(c),我认为您收到错误,因为您需要在那里定义长度以及已经定义的变量。
Answering question (c), I think you're getting the error because you need to define length down there along with the variables you've already defined.