嗨,我正在玩弄創建Java包。Java導入包錯誤
我在名爲admin的文件夾中創建了一個名爲Employee的文件,這個文件正確編譯。在這個包之外,我有另一個導入它的java文件。這是源代碼。
import java.util.*;
// this works --> import admin.Employee;
import admin.*; // this doesn't
public class Hello {
public static void main(String[] args) {
Employee h = new Employee("James", 20000);
System.out.println(h.getName());
}
}
奇怪的是,第二個import語句工作正常,但與第三個我得到
- 無法訪問
Employee
- 糟糕的類文件:
./Employee.class
我只用javac Hello.java編譯
員工類在包管理員中。結構爲
該文件夾外部的文件夾「admin」 - >包含「Employee.class」和「Employee.java」 是hello.java文件。
package admin;
import java.util.*;
public class Employee
{
private static int nextId;
private int id;
private String name = "";
private double salary;
// static initialization block
static
{
Random generator = new Random();
// set nextId to a random number between 0 and 9999
nextId = generator.nextInt(10000);
}
// object initialization block
{
id = nextId;
nextId++;
}
// three overloaded constructors
public Employee(String n, double s)
{
name = n;
salary = s;
}
public Employee(double s)
{
// calls the Employee(String, double) constructor
this("Employee #" + nextId, s);
}
// Default constructor
public Employee()
{
// name initialized to ""--see below
// salary not explicityl set--initialized to 0
// id initialized in initialization block
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
}
您是否將班級Employee公開? – rAy
什麼是Employee類的訪問說明符? – Abubakkar
你能告訴我們'Employee'的源代碼,你的包結構以及你如何編譯這些文件? – NINCOMPOOP