Inner Classes

原创 2005年03月02日 17:28:00

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>

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/faintbear/article/details/308114

inner classes cannot have static declarations

inner classes cannot have static declarationsinner classes cannot have static declarations
  • fyfcauc
  • fyfcauc
  • 2014-12-08 11:57:05
  • 1909

Java中的Nested Classes和Inner Classes

Java中的Nested Classes和Inner ClassesJava有嵌套类(Nested Classes)和内部类(Inner Classes)的概念。 嵌套类(Nested Classe...
  • u010999240
  • u010999240
  • 2015-08-09 02:21:47
  • 1245

在Android Manifest中出现Use '$' instead of '.' for inner classes (or use only lowercase letters in pac

在Android 的Manifest中注册Activity时有时会出现Use '$' instead of '.' for inner classes (or use only lowercase l...
  • Flying_Highter_Birds
  • Flying_Highter_Birds
  • 2014-12-09 15:00:00
  • 2085

Use '$' instead of '.' for inner classes (or use only lowercase letters in package names)

项目menifest突然出现"Use '$' instead of '.' for inner classes (or use only lowercase letters in package ...
  • baohanqing
  • baohanqing
  • 2015-07-25 23:13:56
  • 1107

Activitys, Threads, & Memory Leaks

Activitys, Threads, & Memory Leaks 在Android编程中,一个公认的难题是在Activity的生命周期如何协调长期运行的任务和避免有可能出现的内存泄漏问题。考...
  • Tibib
  • Tibib
  • 2013-08-27 15:13:18
  • 1715

SCJP Sun® Certified Programmer for Java™ 6 Study Guide chapter 8

  • 2010年07月28日 14:22
  • 1.32MB
  • 下载

[OOD-More C++ Idioms] 内部类 (Inner Class)

不用通过多重继承就可以实现多套接口,同时可以自然地向上转换(Up-casting)。 在单个抽象下提供相同接口的多个实现。...
  • HorkyChen
  • HorkyChen
  • 2015-10-21 22:50:47
  • 1892

Nested Class,Inner Class,Member Class,Local Class, and Anonymous class区别

Nested Class,Inner Class,Member Class,Local Class, and Anonymous class区别 其实所有类都可能是top level类或是nest...
  • computerclass
  • computerclass
  • 2007-05-13 18:29:00
  • 990

JAVA菜鸟入门(13) outer class与inner class的之间能互相使用field或者method吗?

Nested classes are divided into two categories: static and non-static.  1. Nested classes that are ...
  • feliciafay
  • feliciafay
  • 2015-05-01 04:44:46
  • 1727

第八章: Interfaces & Inner Classes

第八章: Interfaces & Inner Classes       1、 Interfaces interface Instrument {   // Compile-time constan...
  • xiong2009
  • xiong2009
  • 2005-12-09 18:30:00
  • 833
收藏助手
不良信息举报
您举报文章:Inner Classes
举报原因:
原因补充:

(最多只允许输入30个字)