我的代码不起作用。我收到以下错误:
No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main).
您可以在下面看到我的代码:
class Main {
public class Room {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + etage;
result = prime * result + gebäude;
result = prime * result + raumnummer;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Room other = (Room) obj;
if (etage != other.etage)
return false;
if (gebäude != other.gebäude)
return false;
if (raumnummer != other.raumnummer)
return false;
return true;
}
public int gebäude;
public int etage;
public int raumnummer;
public Room(int gebäude, int etage, int raumnummer) {
super();
this.gebäude = gebäude;
this.etage = etage;
this.raumnummer = raumnummer;
}
@Override
public String toString() {
String s = String.format("%2s-%s.%02d", this.gebäude, this.etage, this.raumnummer);
return s;
}
}
public static void main(String[] args) {
Room office = new Room(17, 0, 10);
Room lecture = new Room(2, 0, 10);
Room lab = new Room(18, 1, 1);
System.out.println(office); // => "17-0.10"
System.out.println(lecture); // => " 2-0.10"
System.out.println(lab); // => "18-1.01"
}
}
最佳答案
解决方案 1
将 Room 类与 Main 类分开对我来说很有效:
文件Main.java:
public class Main {
public static void main(String[] args) {
Room office = new Room(17, 0, 10);
Room lecture = new Room(2, 0, 10);
Room lab = new Room(18, 1, 1);
System.out.println(office); // => "17-0.10"
System.out.println(lecture); // => " 2-0.10"
System.out.println(lab); // => "18-1.01"
}
}
文件室.java:
public class Room {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + etage;
result = prime * result + gebäude;
result = prime * result + raumnummer;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Room other = (Room) obj;
if (etage != other.etage)
return false;
if (gebäude != other.gebäude)
return false;
if (raumnummer != other.raumnummer)
return false;
return true;
}
public int gebäude;
public int etage;
public int raumnummer;
public Room(int gebäude, int etage, int raumnummer) {
super();
this.gebäude = gebäude;
this.etage = etage;
this.raumnummer = raumnummer;
}
@Override
public String toString() {
String s = String.format("%2s-%s.%02d", this.gebäude, this.etage, this.raumnummer);
return s;
}
}
让我调查一下为什么会发生这种情况,然后我会发布原因。到目前为止,如果您不需要两个类位于同一个文件中,那么这是一个合适的修复。
解决方案2
使房间静态:
...
class Main {
public static class Room {
@Override
public int hashCode() {
final int prime = 31;
...
为什么会发生这种情况
如 this post 中所述,通过将 Room 作为 Main 的内部类,您将强制 Room 的实例拥有 Main 的实例。当在内部类 Room 上使用 new 运算符而不创建 Main 的新实例时,会产生错误。
通过将 Room 类设为静态,该类不需要 Main 的实例。
关于java - 错误 : No enclosing instance of type Main is accessible. 必须使用 Main 类型的封闭实例来限定分配(例如 x.new A() wh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58826720/