对java 了解不多,碰到了一个不太明白的问题。
在一个类的静态方法中,为什么不能直接实例化类中包含的非静态的类。
比如一下代码:
public class Hello {
interface ITest
{
void SayHello(String msg);
}
public class CBaseTest
{
public void Hello(String msg)
{
System.out.println("hello" + msg);
}
}
public class CChild extends CBaseTest implements ITest
{
public void SayHello(String msg)
{
System.out.println("Interface" + msg);
}
public CChild()
{
;
}
}
public static void main(String[] args)
{
System.out.println("Hello,Montaque");
CChild c=new CChild();
}
}
CChild c=new CChild()的时候报错。
No enclosing instance of type Hello is accessible. Must qualify the allocation with an enclosing
instance of type Hello (e.g. x.new A() where x is an instance of Hello).
必须要把 CChild 改为静态类或者 从新wrappe 一个方法来调用CChild 的方法。
在一个类的静态方法中,为什么不能直接实例化类中包含的非静态的类。
比如一下代码:
CChild c=new CChild()的时候报错。
No enclosing instance of type Hello is accessible. Must qualify the allocation with an enclosing
instance of type Hello (e.g. x.new A() where x is an instance of Hello).
必须要把 CChild 改为静态类或者 从新wrappe 一个方法来调用CChild 的方法。
浙公网安备 33010602011771号