0

这是一个添加/删除不同类型客户的简单程序。

我知道这可能很简单,但我已经有一段时间了。

不断收到错误,

constructor Customer in class Customer cannot be applied to the given types; required; java.lang. String, Address, char found:no arguments reason: actual and formal argument lists differ in length

我猜这与客户构造函数有关,但不知道。

public class Address
{
    private String street,town;

    /**
     * Constructor for Address
     * 
     * @param street    The street
     * @param town      The town
     */
    public Address (String street, String town)
    {
        this.street = street;
        this.town = town;
    }

    /**
     * @return   The street
     */
    public String getStreet()
    {
        return street;
    }

    /**
     * @return   The town
     */
    public String getTown()
    {
        return town;
    }

    /**
     * Change the street part of the address
     * 
     * @param street    The new street
     */
    public void setStreet(String street)
    {
        this.street = street;
    }

    /**
     * Change the town part of the address
     * 
     * @param street    The new town
     */
    public void setTown(String town)
    {
        this.town = town;
    }

    public String toString()
    {
        return street + "\n" + town;
    }
}
public class Name
{
    private String fName, lName;

    /** 
     * Constructor for Name
     * 
     * @param fName     The first name
     * @param lName     The last name
     */
    public Name(String fName, String lName)
    {
        this.fName = fName;
        this.lName = lName;
    }  

    /** 
     * @return  The first name
     */
    public String getFName()
    {
        return fName;
    }

    /** 
     * @return  The last name
     */
    public String getLName()
    {
        return lName;
    }

    /**
     * Amend the first name
     * 
     * @param fName The new first name
     */
    public void setFName(String fName)
    {
        this.fName = fName;
    }

     /**
     * Amend the last name
     * 
     * @param lName The new last name
     */
    public void setLName(String lName)
    {
        this.lName = lName;
    }


    public String toString()
    {
        return fName + " " + lName;
    }

}
public class Customer
{
    // instance variables - replace the example below with your own
    private String accountNumber;
    private Address address;
    private int balance;
    private char customerType;

    /**
     * Constructor for objects of class Customer
     */
    public Customer(String accountNumber, Address address, char customerType)
    {
        // initialise instance variables
        this.accountNumber= accountNumber;
        this.address= address;
        balance = 0;
        this.customerType=customerType;
    }

    /** 
     * Adds money to the ammount in the account
     */
    public void credit(int amt)
    {
        balance= balance + amt;
    }

    /**
     * removes money from the ammount in the account
     */
    public void debit(int amt)
    {
        balance= balance - amt;
    }

    /**
     * Returns the account number of the customer
     */
    public String getAccountNumber()
    {
        return accountNumber;
    }

    /**
     * returns the address of the customer
     */
    public Address getAddress()
    {
        return address;
    }

    /**
     * returns the balance of the customer
     */
    public int getBalance()
    {
        return balance;
    }

    /**
     * Returns the type of customer Bussiness or Personal
     */
    public char getCustomerType()
    {
        return customerType;
    }

    public void setAddress(Address address)
    {
       this.address=address;
    }

    public String toString()
    {
        String output ="Account Number: " + accountNumber + "Customer Type: " + customerType  + "balance: " + getBalance() + "Address: " + super.toString();
        return output;
    }
}
public class PersonalCustomer extends Customer
{
    // instance variables
    private Name name;

    public PersonalCustomer(String accountNumber, Address address, Name name)
    {
        // initialise instance variables
        this.accountNumber= accountNumber;
        this.address=address;
        this.name= Name;
    }

    public Name getName()
    {
        return + "Address: " + super.toString();
    }

    public void print()
    {
        System.out.println(toString());
    }

    public String toString()
    {
        String output = getFName() + getLName() + "\n" + "Account Number: " + accountNumber + "\n" 
                                + "Customer Type: " + customerType + "\n" + "Address: " + super.toString() + "\n"  
                                            + "balance: " + getBalance();
        return output; 
    }

}
4

2 回答 2

2

错误消息说您正在调用:

new Customer();

但客户需要字符串、地址和字符,例如:

new Customer("Bob", new Address("Cool Street","Awesome Town"), 'a');

这似乎有点奇怪,但原因是如果您不这样做,您的子类会隐式调用父构造函数。

public PersonalCustomer(String accountNumber, Address address, Name name)
{
    // super(); //calls the parent constructor with no arguments without you seeing
    this.accountNumber= accountNumber;
    this.address=address;
    this.name= Name;
}

您需要做的就是将 PersonalCustomer 构造函数更改为

public PersonalCustomer(String accountNumber, Address address, Name name)
{
    super(accountNumber, address, 'p'); //or whatever customer type they are supposed to get
    this.name= Name;
}
于 2013-02-27T20:29:55.503 回答
1

如果您不定义构造函数,则有一个隐式的无参数构造函数。

但是,当你定义一个带参数的构造函数时,隐式的 no-rags 构造函数就会消失。

在定义使用默认构造函数的新构造函数之前,您必须有一些代码,即new Customer(). 您必须定义一个无参数构造函数来解决您的问题

这个小宝石吸引了许多新的 Java 程序员。

于 2013-02-27T20:33:48.690 回答