gpt4 book ai didi

java - Java中扩展类的构造函数

转载 作者:行者123 更新时间:2023-11-29 04:02:12 26 4
gpt4 key购买 nike

我在分配硬件时遇到了一些问题。在一个作业中,我们必须创建一个 Person 类。我的是:

public class Person
{
String firstName;
String lastName;
String telephone;
String email;

public Person()
{
firstName = "";
lastName = "";
telephone = "";
email = "";
}

public Person(String firstName)
{
this.firstName = firstName;
}

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;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getTelephone()
{
return telephone;
}

public void setTelephone(String telephone)
{
this.telephone = telephone;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public boolean equals(Object otherObject)
{
// a quick test to see if the objects are identical
if (this == otherObject) {
return true;
}

// must return false if the explicit parameter is null
if (otherObject == null) {
return false;
}

if (!(otherObject instanceof Person)) {
return false;
}

Person other = (Person) otherObject;
return firstName.equals(other.firstName) && lastName.equals(other.lastName) &&
telephone.equals(other.telephone) && email.equals(other.email);
}

public int hashCode()
{
return 7 * firstName.hashCode() +
11 * lastName.hashCode() +
13 * telephone.hashCode() +
15 * email.hashCode();
}

public String toString()
{
return getClass().getName() + "[firstName = " + firstName + '\n'
+ "lastName = " + lastName + '\n'
+ "telephone = " + telephone + '\n'
+ "email = " + email + "]";
}
}

现在我们必须创建一个使用 Person 作为属性的 Loan 类,然后扩展该 Loan 类。我的贷款类别是:

public abstract class Loan
{
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;
}

public void printPayments()
{
double monthlyInterest;
double monthlyPrincipalPaid;
double newPrincipal;
int paymentNumber = 1;
double monthlyInterestRate = interestRate / 1200;
double monthlyPayment = loanAmount * (monthlyInterestRate) /
(1 - Math.pow((1 + monthlyInterestRate),( -1 * loanLength)));

// amortization table
while (loanAmount != 0) {
monthlyInterest = loanAmount * monthlyInterestRate;
monthlyPrincipalPaid = monthlyPayment - monthlyInterest;
newPrincipal = loanAmount - monthlyPrincipalPaid;
loanAmount = newPrincipal;

System.out.println("Payment Number | Interest | Principal | Loan Balance");
System.out.printf("%d, %.2f, %f, %f", paymentNumber++, monthlyInterest, newPrincipal, loanAmount);
}
}
/*
//method to print first payment
public double getFirstPayment()
{
}

method to print last payment
public double getLastPayment()
{
}*/

private Person client;
private int loanId;
private double interestRate;
private int loanLength;
private double loanAmount;
private static int nextId = 1;

}

然后用CarLoan类扩展了Loan类,有一个函数原型(prototype)为:

public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax,
double interestRate, CAR_LOAN_TERMS length)

我对如何使用父类(super class)中的 Person 构造函数感到困惑。我不一定能做

super(client);

在我的构造函数中,这就是本书在他们的示例中对一些原始类型所做的。不确定正确的做法是什么......有什么想法吗?谢谢!

最佳答案

CarLoan 不应扩展 Person。这是没有意义的,因为 CarLoan 不能是一个人。

但是 Person 可以是 CarLoan 类中的类变量。

public class CarLoan {

private Person client;
private double vehiclePrice;

public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax, double interestRate, CAR_LOAN_TERMS length) {

this.client = client;
this.vehiclePrice = vehiclePrice;
..
}
}

关于java - Java中扩展类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com