我可以将一个静态变量和一个非静态变量相乘,如下所示:
public class C {
protected int c;
private static int s;
public int ma() { return this.c*this.s; }
}
或者:
public class B{
protected int x;
private static int y;
public static int ms() { return x + y; }
}
第二个代码不起作用,我想知道这是因为它期望静态吗?
第二个代码块不起作用,因为ms
是static
。您无法从上下文引用非static
成员(x
)static
。
你需要要么使ms
非static
功能或使x
一个static
变量。
像这样:
public class B{
protected static int x; // now static
private static int y;
public static int ms() { return x + y; }
}
或像这样:
public class B{
protected int x;
private static int y;
public int ms() { return x + y; } // now non-static
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句