我正在对Java类进行介绍,并且坚持从事我的实验室工作。
我们正在建立一个继承类。教授为我们提供了Main
,Address
,PersonName
,PhoneNumber
和PersonRecord
。
我们的任务是创建一个包含客户ID,creditCardType,creditCardNumber和creditCardDate的类CustomerRecord
。我们被指示生成一个类,并对“ CustomerRecord而不是Main”进行必要的更改
我继续为该类设置了所有内容,但继续遇到以下相同错误:
Error:(20, 20) java: constructor CustomerRecord in class edu.cscc.CustomerRecord cannot be applied to given types;
required: java.lang.String,java.lang.String,java.lang.String,java.lang.String
found: edu.cscc.PersonName,edu.cscc.Address,edu.cscc.PhoneNumber,edu.cscc.PhoneNumber,edu.cscc.PhoneNumber,java.lang.String,java.lang.String,java.lang.String,java.lang.String
reason: actual and formal argument lists differ in length
以下是主要内容:
public class Main {
public static void main(String[] args) {
// Initialize test data
Address address = new Address("120 North Tulip Tree Drive",
"Jackson", "OH", "45640");
PersonName name = new PersonName("Dr.", "Adelaide", "Penelope",
"Aardvark", null);
PhoneNumber homephone = new PhoneNumber(740, 555, 1005);
PhoneNumber workphone = new PhoneNumber(740, 555, 2356);
PhoneNumber cellphone = new PhoneNumber(614, 555, 9723);
// TODO - after creating CustomerRecord class, uncomment the following code.
// Create sample customer record
CustomerRecord customer;
customer = new CustomerRecord (name, address, homephone, workphone, cellphone,
"123456","Visa","4111-1111-1111-1111", "12/25");
// Print customer record report
String namerpt = "Name: " + customer.getName().toString();
String addressrpt = "Address: " + address.getStreetAddress() + "\n" +
"\t" + address.getCity() + ", " + address.getState() + " " + address.getZip();
String phonerpt = "Home Phone: " + customer.getHomePhone().toString() + "\n" +
"Work Phone: " + customer.getWorkPhone().toString() + "\n" +
"Mobile Phone: " + customer.getCellPhone().toString();
System.out.println(namerpt+"\n"+addressrpt+"\n"+phonerpt+"\n"+
"Customer ID: "+customer.getCustomerID() + "\n"+
"Credit card type: "+customer.getCreditCardType() + "\n"+
"Credit card number: "+customer.getCreditCardNumber() + "\n"+
"Credit card date: "+customer.getCreditCardDate());
}
}
以下是我创建的Customer类:
public class CustomerRecord {
private String customerID;
private String creditCardType;
private String creditCardNumber;
private String creditCardDate;
public CustomerRecord(String customerID, String creditCardType, String creditCardNumber, String creditCardDate) {
this.customerID = customerID;
this.creditCardType = creditCardType;
this.creditCardNumber = creditCardNumber;
this.creditCardDate = creditCardDate;
}
//Accesor//Mutator
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String customerID) {
this.customerID = customerID;
}
public String getCreditCardType() {
return creditCardType;
}
public void setCreditCardType(String creditCardType) {
this.creditCardType = creditCardType;
}
public String getCreditCardNumber() {
return creditCardNumber;
}
public void setCreditCardNumber(String creditCardNumber) {
this.creditCardNumber = creditCardNumber;
}
public String getCreditCardDate() {
return creditCardDate;
}
public void setCreditCardDate(String creditCardDate) {
this.creditCardDate = creditCardDate;
}
}
最佳答案
由于不应该更改Main
类,因此请添加必需的字段并在CustomerRecord
类中编辑构造函数,如下所示:
private PersonName name;
private Address address;
private PhoneNumber homephone;
private PhoneNumber workphone;
private PhoneNumber cellphone;
private String customerID;
private String creditCardType;
private String creditCardNumber;
private String creditCardDate;
public CustomerRecord(PersonName name, Address address, PhoneNumber homephone, PhoneNumber workphone, PhoneNumber cellphone, String customerID, String creditCardType, String creditCardNumber, String creditCardDate) {
this.name = name;
this.address = address;
this.homephone = homephone;
this.workphone = workphone;
this.cellphone = cellphone;
this.customerID = customerID;
this.creditCardType = creditCardType;
this.creditCardNumber = creditCardNumber;
this.creditCardDate = creditCardDate;
}
这样,即可处理
CustomerRecord
构造函数的所有参数。