让主类不作为超类怎么办?

时间:2019-02-26 17:02:57

标签: java

在这里,我有一个名为“ Staff”的超类。我的主要方法位于一个单独的名为“ Program_2A”的类中。给定的文件名是Program_2A.java。 Eclipse在程序的第二行显示

错误
  

链接所有引用以进行本地重命名(不会更改其他文件中的引用)

我不理解拥有主类而不是作为超类有什么问题。

代码如下:

import java.util.*;

public class Staff {
    private int Staff_ID;
    private String Name;
    private int Phone;
    private int Salary;

    public Staff(int staff_id, String name, int phone, int salary)
    {
        Staff_ID = staff_id;
        Name = name;
        Phone = phone;
        Salary = salary;
    }

    public void display()
    {
        System.out.println("\t" + Staff_ID + "\t" + Name + "\t" + Phone + "\t" + Salary);
    }
}
class Teaching extends Staff
{
    private String Domain;
    private int Publication;

    public Teaching(int staff_id, String name, int phone, int salary, String domain, int publication) {
        super(staff_id,name,phone,salary);
        Domain = domain;
        Publication = publication;
    }
    public void display() {
        super.display();
        System.out.println("\t" + Domain + "\t" + Publication);
    }
}
class Technical extends Staff
{
    private String Skills;
    public Technical(int staff_id, String name, int phone, int salary, String skills) {
        super(staff_id,name,phone,salary);
        Skills = skills;
    }
    public void display() {
        super.display();
        System.out.println("\t" + Skills);
    }
}
class Contract extends Staff
{
    private int Contract;
    public Contract(int staff_id, String name, int phone, int salary, int contract) {
        super(staff_id,name,phone,salary);
        Contract = contract;
}
    public void display() {
        super.display();
        System.out.println("\t" + Contract);
    }
}

class Program_2A {
    public static void main(String[] args) {
        Staff St[] = new Staff[3];
        St[0] = new Teaching(1, "ABC", 1234, 10000, "CSE", 3);
        St[1] = new Technical(2, "DEF", 5678, 200000, "C++");
        St[2] = new Contract(3, "GHI", 9012, 50000, 3);

        System.out.println("STAFF ID \t NAME \t PHONE \t SALARY \t DOMAIN \t PUBLICATIONS \t SKILLS \t PERIOD");
        for(int i=0;i<3;i++) {
            St[i].display();
            System.out.println();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

在Java中,一个文件只能包含一个公共类。

因此,请将class Program_2A更改为public class Program_2A,并在public类之前删除Staff关键字。