gpt4 book ai didi

java - 无法编译个人客户类,构造函数有问题吗?

转载 作者:行者123 更新时间:2023-12-01 23:59:23 25 4
gpt4 key购买 nike

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

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

继续出现错误,

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

}

最佳答案

错误消息显示您正在调用:

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

关于java - 无法编译个人客户类,构造函数有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15121706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com