montaque

小碗喝酒,小口吃肉

博客园 首页 新随笔 联系 订阅 管理
对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 的方法。
posted on 2004-11-20 12:47  montaque  阅读(15166)  评论(7)    收藏  举报