遇到了很多麻烦;非法开始表达

时间:2015-09-30 20:21:59

标签: java netbeans

我对计算机编程非常陌生 - 已经在6周前开始了这个课程 - 我目前在Netbeans中非法启动表达式时遇到了麻烦。

整个代码如下(因为我甚至不知道从哪里开始):

public class Employees {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        public class Employee

        //properties
        private String name;
        private String ID;
        private String salary;

        //constructor
        public Employee (String name, String address, String dob) {
            this.name = name;
            this.ID = ID;
            this.salary = salary;
        }

        //method to print details on employees
        public void printDetails() {
            System.out.println("Employee name: " +this.name);
            System.out.println("ID: "+ this.ID);
            System.out.println("Annual Salary: " + this.salary);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您不能在方法声明(public class Employee)中声明类(public static void main(String[] args) {)。

最好将class Employee的声明移动到自己的文件(Employee.java)。如果您不想将其移至其他文件,也可以将其移至现有文件的 end 。但是你必须声明它private而不是public

或者你可以把这一切都变成这样的一类:

public class Employees {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Employees employee = new Employees("name", "address", "dob");
        employee.printDetails();
    }

    //properties
    private String name;
    private String ID;
    private String salary;

    //constructor
    public Employees (String name, String address, String dob) {
        this.name = name;
        this.ID = ID;
        this.salary = salary;
    }

    //method to print details on employees
    public void printDetails() {
        System.out.println("Employee name: " +this.name);
        System.out.println("ID: "+ this.ID);
        System.out.println("Annual Salary: " + this.salary);
    }
}