在Eclipse中编译的员工电话程序,错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at pcsettingfirstpj.Ch8_1_1.main(Employee.java:34)
与程序:
package pcsettingfirstpj;
public class Employee {
private char name;
private Phone list;
class Phone {
private long home, cell;
public Phone(long phone1, long phone2){
home = phone1; cell = phone2;
}
public void printPhone(){
System.out.println("Tel (Home 2): " + home);
System.out.println("Tel (Mobile 2): " + cell);
}
}
public Employee(char name, long home, long cell){
this.name = name;
list = new Phone(home, cell);
}
public void printEmployee(){
System.out.println("====Staff====");
System.out.println("Name: " + name);
System.out.println("Tel (Home 1): " + list.home);
System.out.println("Tel (Mobile 1): " + list.cell);
list.printPhone();
}
}
public class Ch8_1_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee joe = new Employee('a',28456789L, 936555552L);
Employee jane = new Employee('b',23134563L,938444444L);
joe.printEmployee();
jane.printEmployee();
}
}
答案 0 :(得分:3)
您似乎已在单个文件中定义了多个公共类,这会导致编译错误,因为Java无法在同一文件中编译两个相邻的公共类。如果这是您的意图,则可以删除public
修饰符以使其package-private。
将此文件命名为Ch8_1_1.java
。
public class Ch8_1_1 {
public static void main(String[] args) {
Employee joe = new Employee('a', 28456789L, 936555552L);
Employee jane = new Employee('b', 23134563L, 938444444L);
joe.printEmployee();
jane.printEmployee();
}
}
class Employee {
private char name;
private Phone list;
class Phone {
private long home, cell;
public Phone(long phone1, long phone2) {
home = phone1;
cell = phone2;
}
public void printPhone() {
System.out.println("Tel (Home 2): " + home);
System.out.println("Tel (Mobile 2): " + cell);
}
}
public Employee(char name, long home, long cell) {
this.name = name;
list = new Phone(home, cell);
}
public void printEmployee() {
System.out.println("====Staff====");
System.out.println("Name: " + name);
System.out.println("Tel (Home 1): " + list.home);
System.out.println("Tel (Mobile 1): " + list.cell);
list.printPhone();
}
}
答案 1 :(得分:0)
请尝试按照以下步骤操作:
Ch8_1_1.java
和Employee.java
可能在同一个包中。Ch8_1_1.java
作为java应用程序运行。 问题是你在同一个file.java中有多个公共类
答案 2 :(得分:0)
每当一个类被定义为公共类时,它应该在一个具有相同名称的文件中。
例如:公共类员工
FileName:Employee.java
在你的情况下,Employee是公共的,因此编译器希望它是Employee.java来生成Employee.class
..但是在同一个类中你还有一个公共类Ch8_1_1
,所以它也想要文件名为Ch8_1_1.java
以生成无法发生的Ch8_1_1.class
..
声明Employeee
为class Employee
而没有公开修饰符..
不要在同一个文件中声明两个公共类..
package pcsettingfirstpj;
class Employee {
private char name;
private Phone list;
class Phone {
private long home, cell;
public Phone(long phone1, long phone2){
home = phone1; cell = phone2;
}
public void printPhone(){
System.out.println("Tel (Home 2): " + home);
System.out.println("Tel (Mobile 2): " + cell);
}
}
public Employee(char name, long home, long cell){
this.name = name;
list = new Phone(home, cell);
}
public void printEmployee(){
System.out.println("====Staff====");
System.out.println("Name: " + name);
System.out.println("Tel (Home 1): " + list.home);
System.out.println("Tel (Mobile 1): " + list.cell);
list.printPhone();
}
}
public class Ch8_1_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee joe = new Employee('a',28456789L, 936555552L);
Employee jane = new Employee('b',23134563L,938444444L);
joe.printEmployee();
jane.printEmployee();
}
}