代码之家  ›  专栏  ›  技术社区  ›  Crystal

吸气剂/定位器(构图,Java,HW)

  •  4
  • Crystal  · 技术社区  · 15 年前

    我有一个班级叫人,基本上看起来像:

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

    我必须和卡洛安一起延长贷款期限,看起来:

    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)考虑到我在个人类中所做的,我在贷款类中对setclient所做的是否正确?(例如,this.client=client)

    (b)我可以在一个方法中调用super两次吗?我必须从Carloan类的构造函数中设置Loan类的两个属性,我认为这是一种实现这一点的方法。

    (C)您必须在构造函数或getter/setter方法中为枚举类型设置不同的属性吗?我在Carloan类中得到一个(this.length=length)错误,我不确定应该如何设置枚举值。谢谢!

    3 回复  |  直到 12 年前
        1
  •  2
  •   Samir Talwar PruthviRaj Reddy    15 年前

    好,按顺序:

    1. setClient 看起来很好。没什么问题。但是,您要避免设置 this.client 直接在 CarLoan 施工员,你已经打过电话了 设置客户端 (感谢@gabriel和@aeth)。
    2. 当然,你可以用 super 尽可能多地访问父类方法。你要小心的是叫超类 构造函数 ,在子类的构造函数的开头只能执行一次。 super != super() .
    3. 不, this.length = length 很好。问题是您没有名为 length . 您可能需要添加其中一个。
        2
  •  1
  •   Stephen C    15 年前

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

    这种方法更好的原因是 贷款 类负责它的初始化,而不依赖不同的子类构造函数来完成该任务。(如果在 贷款 并将相应的参数添加到 贷款 构造函数,编译器提醒您修改所有子类构造函数,以便在 super 构造函数链接。如果子类负责在初始化期间设置基类中的字段,那么编译器不会注意到您忘记添加新的setter调用。)

    4)如果确实在构造函数中调用方法,那么最好确保它们不能在子类中被重写。(不…方法被重写并不是完全错误的,但是有些事情可能会非常错误。在构造函数中调用可能可重写的方法会使代码脆弱。)

    5)如果这是生产代码,使用 float double 代表货币价值将是一个巨大的不,不!

        3
  •  0
  •   Nick    15 年前

    回答问题(c),我认为你得到了错误,因为你需要定义长度以及你已经定义的变量。