java inner class

原创 2004年08月29日 14:30:00
2004-3-21 星期日(Sunday) 小雨

克隆方法
如果这个类是可以被克隆的,那么下一步就是 clone 方法:

public
Object clone() {
 try {
 CounterSet obj = (CounterSet)super.clone();
 obj.packets = (int[])packets.clone();
 obj.size = size;
 return obj;
 }catch(CloneNotSupportedException e) {
 throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage());
 }
}


toString 方法
无论如何,每一个类都应该定义 toString 方法:

public
String toString() {
 String retval = "CounterSet: ";
 for (int i = 0; i < data.length(); i++) {
 retval += data.bytes.toString();
 retval += data.packets.toString();
 }
 return retval;
 }
}


不必要的对象构造
不要在循环中构造和释放对象


使用 StringBuffer 对象
在处理 String 的时候要尽量使用 StringBuffer 类,StringBuffer 类是构成 String 类的基础。String 类将 StringBuffer 类封装了起来,(以花费更多时间为代价)为开发人员提供了一个安全的接口。当我们在构造字符串的时候,我们应该用 StringBuffer 来实现大部分的工作,当工作完成后将 StringBuffer 对象再转换为需要的 String 对象。比如:如果有一个字符串必须不断地在其后添加许多字符来完成构造,那么我们应该使用 StringBuffer 对象和她的 append() 方法。如果我们用 String 对象代替 StringBuffer 对象的话,会花费许多不必要的创建和释放对象的 CPU 时间。


Note three items in CloneDemo1. First, the throws CloneNotSupportedException clause attached to main()'s signature means you wish to ignore any CloneNotSupportedException objects thrown from clone(). Those exception objects are thrown in one of two situations -- either you overrode clone() and purposely threw the exception in a subclass (to prevent subclass objects from being cloned) or you called Object's clone() method from code in a class that does not also implement the Cloneable interface.

As you can probably guess, the second item to note is the implements Cloneable clause. Cloneable is an interface -- a concept I will discuss in next month's article. For now, keep in mind that an interface resembles a class, but can only declare method signatures and constants. If you examine the documentation on Cloneable, you discover that the interface is empty. You must specify implements Cloneable in the class declaration for those classes whose code calls Object's clone() method. Behind the scenes, Cloneable helps Object's clone() method identify those subclasses that must have their field values duplicated. (Object's clone() method throws a CloneNotSupportedException object if a class, whose code calls that method, does not implement Cloneable.)

The final item to note is the comment line in CloneDemo1. That line attempts to clone an AnotherClass object. I inserted that comment because the line of code won't compile. By default, you cannot clone an object from another class. To do so, you must first override clone(), as Listing 6 demonstrates:


If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName, as seen in main( ).


OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.getInnerClass();
ic.showMe();
OuterClass.InnerClass ic2 = oc.new InnerClass();
ic2.showMe();

Thus, the private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation. In addition, extension of an interface is useless from the client programmer’s perspective since the client programmer cannot access any additional methods that aren’t part of the public interface. This also provides an opportunity for the Java compiler to generate more efficient code.

Normal (non-inner) classes cannot be made private or protected; they may only be given public or package access.

If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final, like the argument to dest( ). If you forget, you’ll get a compile-time error message. Feedback

In this case, the variable i did not have to be final. While i is passed to the base constructor of the anonymous class, it is never directly used inside the anonymous class. Feedback


Here’s the “parcel” theme with instance initialization. Note that the arguments to dest( ) must be final since they are used within the anonymous class:

So an inner class has automatic access to the members of the enclosing class. How can this happen? The inner class must keep a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that (hidden) reference is used to select that member. Fortunately, the compiler takes care of all these details for you, but you can also understand now that an object of an inner class can be created only in association with an object of the enclosing class. Construction of the inner class object requires the reference to the object of the enclosing class, and the compiler will complain if it cannot access that reference. Most of the time this occurs without any intervention on the part of the programmer.


If you don’t need a connection between the inner class object and the outer class object, then you can make the inner class static. This is commonly called a nested class.[36] To understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner class implicitly keeps a reference to the object of the enclosing class that created it. This is not true, however, when you say an inner class is static. A nested class means: Feedback


You don’t need an outer-class object in order to create an object of a nested class. Feedback
You can’t access a non-static outer-class object from an object of a nested class. Feedback

 However, if you make a nested class (a static inner class), then it doesn’t need a reference to the outer class object


Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.

why inner class?
Without the ability that inner classes provide to inherit—in effect—from more than one concrete or abstract class, some design and programming problems would be intractable. So one way to look at the inner class is as the rest of the solution of the multiple-inheritance problem. Interfaces solve part of the problem, but inner classes effectively allow “multiple implementation inheritance.” That is, inner classes effectively allow you to inherit from more than one non-interface. Feedback

java 内部类(inner class)详解

一、为何使用内部类 内部类提供了更好的封装,只有外部类能访问内部类 内部类可以独立继承一个接口,不受外部类是否继承接口影响 内部类中的属性和方法即使是外部类也不能直接访问,相反内部类可以直接访问外部...
  • suifeng3051
  • suifeng3051
  • 2016年06月30日 17:30
  • 4016

【java面试题】Static Nested Class(嵌套类) 和 Inner Class(内部类)的不同 、final、static

nested class(一般是c++的说法),Inner class(一般是java的说法)。JAVA内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上。 具体可见http://java....
  • Angiexia
  • Angiexia
  • 2014年03月04日 14:25
  • 2650

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
  • 1587

为什么必须是final的呢?

一个谜团 如果你用过类似guava这种“伪函数式编程”风格的library的话,那下面这种风格的代码对你来说应该不陌生: 1 2 3 4 5 6 7 8 9 ...
  • cuipengfei1
  • cuipengfei1
  • 2013年06月23日 00:57
  • 8265

Java内部类(Inner Class)

简单的说,内部(inner)类指那些类定义代码被置于其它类定义中的类;而对于一般的、类定义代码不嵌套在其它类定义中的类,称为顶层(top-level)类。对于一个内部类,包含其定义代码的类称为它的外部...
  • petib_wangwei
  • petib_wangwei
  • 2014年08月29日 13:31
  • 292

静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同

答:Static Nested Class是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化。而通常的内部类需要在外部类实例化后才能实例化,其语法看起来挺诡异的,如下所示。 ...
  • zzy7075
  • zzy7075
  • 2015年12月22日 10:53
  • 3071

dex2jar源码解析----解析dex文件<二>

接上面,我们继续分析doTranslate的下一部分private void doTranslate(final Path dist) throws IOException { De...
  • new_abc
  • new_abc
  • 2016年12月21日 15:16
  • 525

Java内部类(Inner Class)详解

简单的说,内部(inner)类指那些类定义代码被置于其它类定义中的类;而对于一般的、类定义代码不嵌套在其它类定义中的类,称为顶层(top-level)类。对于一个内部类,包含其定义代码的类称为它的外部...
  • brice2010
  • brice2010
  • 2013年07月20日 16:24
  • 214

浅谈Java内部类(inner class)

Java的内部类可以分为四种:成员内部类(“regular” inner class),静态内部类(static nested class),局部内部类(method-local inner cla...
  • peflapos
  • peflapos
  • 2015年08月07日 00:30
  • 116

内部类(Inner Class)

内部类(Inner Class) 内部类共有四种: 静态内部类(Static Inner Class):只能访问外部类的静态成员变量与静态方法,声明方式如下:public class Outer...
  • MRJJR007
  • MRJJR007
  • 2014年03月04日 22:17
  • 381
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java inner class
举报原因:
原因补充:

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