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

コンストラクターを定義しない場合、暗黙的な引数なしのコンストラクターがあります。

ただし、引数を使用してコンストラクターを定義すると、暗黙のラグなしコンストラクターが消えます。

デフォルトのコンストラクターを使用する新しいコンストラクターを定義する前に、いくつかのコードが必要ですnew Customer()問題を解決するには、引数なしのコンストラクターを定義する必要があります

この小さな宝石は、多くの新しい Java プログラマーを引き付けます。

于 2013-02-27T20:33:48.690 に答える