随笔-144  评论-80  文章-1  trackbacks-0

package com.faintbear;

public class Outer4
{
  private int size = 5;
  public Object makeTheInner( final int localVar )
               {
                   final int finalLocalVar = 6;
                   class Inner
                       {
                          public String toString()
                              {
                            return ( "localVar=" + localVar + ">" );
                              }//end public String toString()
                        }//end class Inner
                  return new Inner();
               }//end public Object makeTheInner( int localVar )
              
public static void main( String args[] )
{
 Outer4 outer = new Outer4();
 Object obj = outer.makeTheInner( 47 );
 System.out.println( "The object is " + obj );
}
}

Inner的toString会被调用,这是肯定的.
你的 final int finalLocalVar = 6; 可以加个static,这样可以节省一定的内存和CPU开销. 不过好像没有用到么,那就删除吧.

同意楼上的
Object obj = outer.makeTheInner( 47 );
System.out.println( "The object is " + obj );
这时其实是System.out.println( "The object is " + obj.toString());

Object obj = outer.makeTheInner( 47 );
等同与
Object obj =new Inner();/////////示意一下
toString方法已经在Inner类中被重写了
所以输出应该是The object is localvar=47>

posted on 2005-03-30 21:40 小力力力 阅读(284) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问