2010-04-25 144 views
1

我在配置hw时遇到了一些麻烦。在一项任务中,我们必须创建一个Person类。我的是:Java中扩展类的构造函数

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 + "]"; 
    } 
} 

现在我们要创建一个使用人作为一个属性的贷款类,然后扩展该贷款类。我的贷款类是:

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类贷款类,有一个函数原型:

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

我很困惑我如何使用Person构造从超。我不一定能做到

super(client); 

在我的构造函数是什么书了一些基本类型中的例子一样。不知道正确的做法是...有什么想法?谢谢!

+1

是否CarLoan类扩展的人吗? – Finbarr 2010-04-25 19:55:56

+0

对不起,贷款延期人,Carloan延期贷款 – Crystal 2010-04-25 20:10:16

回答

3

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; 
    .. 
    } 
} 
+0

我认为客户应该是贷款的受保护成员。 – NomeN 2010-04-25 21:03:48

+0

@NomeN:如果客户是在贷款申报,那么它应该受到保护。但是当CarLoan没有扩展类的时候,它并没有增加任何价值。 – Espen 2010-04-26 08:47:40

+0

OP在他的文章中提到人应该是贷款的属性,i.o.w.贷款中应该有一个Person类型的字段。因为您需要从CarLoan访问此字段进行设置,所以您需要为此字段获取/设置方法或使其受到保护。 – NomeN 2010-04-26 10:14:30

0

如果CarLoan要延伸Person,则Person成为CarLoan的超类。

CarLoan构造函数中,你必须始终通过super关键字调用构造函数Person的任何其他处理发生之前。

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

1

它看起来像你想用composition而不是继承。

用普通英语,CarLoan 有一个客户端(Person类型)。 CarLoan本身不是一个人(继承会提示)。

所以你应该做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; 
    } 

} 
相关问题