我研究如何制作一个接受任何对象的参数,我找到了答案,我尝试重新创建代码。但问题是每当我初始化Bee,Horse和Apple时,它总是显示错误"非静态变量无法从静态上下文初始化"。那怎么回事呢?
public class Testing{
public static void main(String[]args){
Bee a= new Bee();
Horse b= new Horse();
Apple c= new Apple():
}
private interface holder{
public int getX();
}
private class Bee implements holder{
int a=52;
public int getX(){
return a;
}
}
private class Horse implements holder{
int a=62;
public int getX(){
return a;
}
}
答案 0 :(得分:0)
只需更改username id daySince numberOfRecord
rob 2D8836 11 7
rob 2D8836 12 7
rob 2D8836 13 7
rob 2D8836 14 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 15 7
rob 2D8836 16 7
rob 2D8836 16 7
和Bee
的类定义:
Horse
为:
private class Bee implements Holder {...}
没有private static class Bee implements Holder {...}
关键字,该类被视为内部类,需要实例化封闭类的实例。 E.g没有static
你必须写:
static
哪个没有意义,因为Testing testing = new Testing();
Bee bee = testing.new Bee();
和Testing
并不真正相关。因此,使用Bee
可以像您尝试的那样完成:
static