我是Java的新手,我能够编译两个Java文件(即BicycleRegistration.java和Bicycle.java。
但是当我尝试运行BicycleRegistration.java时,出现以下错误
Exception in thread "main" java.lang.IllegalAccessError: failed to access class Bicycle from class BicycleRegistration (Bicycle is in unnamed module of loader 'app'; BicycleRegistration is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @2eda0940)
at BicycleRegistration.main(BicycleRegistration.java:8)
保存在BicycleRegistration.java中:
class BicycleRegistration{
public static void main(String[] args){
Bicycle bike1, bike2;
String owner1, owner2;
bike1 = new Bicycle(); // Create and assign values to bike1
bike1.setOwnerName("Adam Smith");
bike2 = new Bicycle(); // Create and assign values to bike2
bike2.setOwnerName("Ben Jones");
// output the information
owner1 = bike1.getOwnerName();
owner2 = bike2.getOwnerName();
System.out.print(owner1 + "owns a bicycle.");
System.out.print(owner2 + "also owns a bicycle");
}
}
保存在Bicycle.java中:
class Bicycle{
//Data member
private String ownerName;
//Constructor: Initialise the data member
public Bicycle(){
ownerName = "Unassigned";
}
//Returns the name of this bicycle's owner
public String getOwnerName(){
return ownerName;
}
//Assigns the name of this bicycle owner
public void setOwnerName(String name){
ownerName = name;
}
}