Java Can';不要创建类的实例
尝试实例化类时出现包含的封闭实例的原因是什么 下面是我的实际代码:Java Can';不要创建类的实例,java,Java,尝试实例化类时出现包含的封闭实例的原因是什么 下面是我的实际代码: public static void main(String[] args) { InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.Item_Type(); // Error: an enclosing instance that contains InterspecTradeItems_Type.Item_Type
public static void main(String[] args) {
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.Item_Type();
// Error: an enclosing instance that contains InterspecTradeItems_Type.Item_Type is required
}
public class InterspecTradeItems_Type {
public class Item_Type {
}
}
谢谢。假设在名为
Main
的类中声明/定义了InterspecTradeItems\u Type
,则需要
InterspecTradeItems_Type.Item_Type item = new Main().
new InterspecTradeItems_Type().new Item_Type();
你有一个内部类和内部类。您需要每个外部类的一个实例才能访问它。假设在名为
Main
的类中声明/定义了InterspecTradeItems\u Type
,您需要
InterspecTradeItems_Type.Item_Type item = new Main().
new InterspecTradeItems_Type().new Item_Type();
你有一个内部类和内部类。要访问它,您需要每个外部类的实例。由于
项类型类不是静态嵌套类,而是谱间项类型的内部类,因此您需要后者的实例来访问前者
因此,要创建内部类的实例,您应该创建封闭类的实例:
new InterspecTradeItems_Type().new Item_Type();
当然,另一个选项是使项目类型
a静态
类:
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
然后您的代码就可以正常工作。因为项类型
类不是静态嵌套类,而是谱间项类型
的内部类,所以您需要后者的实例来访问前者
因此,要创建内部类的实例,您应该创建封闭类的实例:
new InterspecTradeItems_Type().new Item_Type();
当然,另一个选项是使项目类型
a静态
类:
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
然后您的代码就可以正常工作。因为项类型是内部类。要实例化内部类,必须首先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
InterspecTradeItems_-Type.Item_-Type Item=新的InterspecTradeItems_-Type()。新的Item_-Type()
因为Item\u Type
是内部类。要实例化内部类,必须首先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
InterspecTradeItems_-Type.Item_-Type Item=新的InterspecTradeItems_-Type()。新的Item_-Type()
要实例化内部类,必须首先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
因此,要么使用上述语法,要么使Item\u类型
class静态
public static void main(String[] args) {
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.
Item_Type();
// Error: an enclosing instance that contains
InterspecTradeItems_Type.Item_Type is required
}
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
有关详细信息,请阅读内部类。要实例化内部类,必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
因此,要么使用上述语法,要么使Item\u类型
class静态
public static void main(String[] args) {
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.
Item_Type();
// Error: an enclosing instance that contains
InterspecTradeItems_Type.Item_Type is required
}
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
阅读内部类了解更多信息。试试看
public static void main(String[] args) {
InterspecTradeItems_Type item = new InterspecTradeItems_Type();
Item_Type item1 = item.new Item_Type();
}
试一试
拥有内部类对象的正确方法是
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.new Item_Type();
拥有内部类对象的正确方法是
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.new Item_Type();
... 或使项目类型为静态。还要注意,Item\u Type
是一个嵌套类。所有内部类都是根据定义嵌套的。JLS第8.1.3节:“内部类是不是显式或隐式声明为静态的嵌套类。”请注意,因为InterspecTradeItems\u Type
不是static
,所以需要外部类的实例来实例化它。@SotiriosDelimanolis。您从何处推断出InterspecTradeItems\u Type
是某个外部类的内部类?它似乎与main
@SotiriosDelimanolis位于同一个类体中。我们不能确定。但是无论如何,如果是这样的话,那么是的,需要另一个封闭的实例。。。。或使项目类型为静态。还要注意,Item\u Type
是一个嵌套类。所有内部类都是根据定义嵌套的。JLS第8.1.3节:“内部类是不是显式或隐式声明为静态的嵌套类。”请注意,因为InterspecTradeItems\u Type
不是static
,所以需要外部类的实例来实例化它。@SotiriosDelimanolis。您从何处推断出InterspecTradeItems\u Type
是某个外部类的内部类?它似乎与main
@SotiriosDelimanolis位于同一个类体中。我们不能确定。但无论如何,如果是这种情况,则需要另一个封闭实例。可能重复的如果您不需要Item_类型中封闭类的实例,则应将其设置为静态。可能重复的如果您不需要Item_类型中封闭类的实例,则应将其设置为静态。