import java.util.*;
class Business {
private String name, phone;
private int employees, age;
void Business(){
name = "Foo Inc.";
phone = "";
employees = 0;
age = 0;
System.out.println("In default constructor!");
printVals();
}
void Business(String name, String phone, int employees){
System.out.printf("%s %s %d\n", name, phone, employees);
this.name = name;
this.phone = phone;
this.employees = employees;
this.age = 0;
System.out.println("In constructor!");
printVals();
}
void printVals(){
System.out.printf("name: %s\n", name);
System.out.printf("phone: %s\n", phone);
System.out.printf("employees: %d\n", employees);
System.out.printf("age: %d\n", age);
}
public static void main(String [] args)
{
Business[] mall = new Business[5];
String[] names = {"The Gap",
"Savers",
"Academy of Salon Professionals",
"Ron's Farmhouse"};
String[] phones = {"555-555-5555", "555-555-5556",
"555-555-5557", "555-555-5558"};
int[] emps = {20, 24, 75, 32};
int i, num = 4;
mall[num - 1] = new Business();
for(i = 0; i < num; i++){
mall[i] = new Business(names[i], phones[i], emps[i]);
System.out.printf("init Business: %s %s %d\n",
names[i], phones[i], emps[i]);
}
}
}
这是来自javac的输出错误:
Business.java:51: cannot find symbol
symbol : constructor Business(java.lang.String,java.lang.String,int)
location: class Business
mall[i] = new Business(names[i], phones[i], emps[i]);
^
1 error
我不明白为什么它告诉我它找不到构造函数。据我所知,参数是相同的......
假设我注释掉了Business()调用的参数,这样它只调用默认的构造函数。至少它编译的方式,但是,方法中的System.out.print *都不会打印任何东西!
任何帮助都将不胜感激。
答案 0 :(得分:6)
您正在使用void Business
定义构造函数。构造函数没有返回类型,因此只需在Business“构造函数”和new
能够找到它们之前删除void。
/* remove void */
Business(String name, String phone, int employees){
System.out.printf("%s %s %d\n", name, phone, employees);
this.name = name;
this.phone = phone;
this.employees = employees;
this.age = 0;
System.out.println("In constructor!");
printVals();
}
答案 1 :(得分:0)
从构造函数中删除void