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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Context泄漏:Handlers & Inner Classes

先思考下面一个代码片段 publicclass SampleActivity extends Activity { private final Handler mLeakyHan...
  • Tibib
  • Tibib
  • 2013-08-26 16:33
  • 1680

JAVA 复习(Think In Java, 4th) -- Inner Classes - Part2

Anonymous Inner Class 一般耒说,我们先实现接口,实现了以後,我们再从事相关的数据运算,如: public class AInner { public interfac...

Inner Classes Example

Inner Class Example To see an inner class in use, first consider an array. In the following exa...

Java Basics Part 20/20 - Inner Classes

Java Basics Part 20/20 - Inner Classes目录 Java Basics Part 2020 - Inner Classes 目录 嵌套类 语法 非静态内部类 内部类 ...

Gson使用方法之Serializing Inner Classes

Serializing Inner Classes 代码测试项包括

JAVA 复习(Think In Java, 4th) -- Inner Classes - Part1

public class EasyInnerClass { private People list[] = null; private int count = 0; publi...

Context是怎么泄露的:Handlers & Inner Classes

原文链接 : How to Leak a Context: Handlers & Inner Classes原文作者 : Alex Lockwood译文出自 : 开发技术前线 www.devtf.cn...

Kotlin-16.嵌套类/内部类(Nested Classes/Inner classes)

嵌套类(Nested Classes),类可以嵌套在其他类中,不能访问外部类成员,相当于java的static 静态内部类;内部类(Inner classes),类标记为inner,可以访问外部类成员...

Nested Classes和Inner Classes

Nested classes 是指定义在类内部的类,Nested classes分两类:静态与非静态,静态叫static nested classes,非静态的叫inner claess。 cl...

Nested Classes,Static Classes,Non-static Classes,Inner Classes,Local Classes,Anonymous Classes

Nested Classes 嵌套类,包括Static Classes静态类和Inner Classes
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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