java inner class

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值