繁体   English   中英

Java中扩展类的构造函数

[英]Constructors from extended class in Java

我在执行任务时遇到了一些麻烦。 在一项任务中,我们必须创建一个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类,有一个函数原型:

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

我对如何使用超类中的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;
    ..
  }
}

看起来你想要使用组合而不是继承。

简单来说,CarLoan 有一个客户(Person类型)。 CarLoan本身不是Person(继承会建议)。

所以你应该做Espen所建议的(组合),而不是CarLoan extends Person (继承)。

可能合法使用的继承是:

class Waiter extends Person {
    String employeeId;

    // A waiter is a person with some extra information
    public Waiter(String firstName, String lastName, String telephone, 
                  String email, String employeeId) {
      super(firstName, lastName, telephone, email); // must be first
      this.employeeId = employeeId;
    }

}

如果CarLoan要扩展Person ,那么Person将成为CarLoan的超类。

CarLoan的构造函数中,您必须始终通过super关键字调用其中一个Person构造函数,然后才能进行任何其他处理。

但是,在我看来,你必须感到困惑,因为你的原型方法将Person的实例传递给CarLoan 此外,我不明白为什么一个名为CarLoan会扩展一个人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM