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的实例及作用分析

 http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html1 首先看代码/**//* * DataStructure....
  • longronglin
  • longronglin
  • 2008-03-01 10:45:00
  • 2307

java Anonymous Inner Class(匿名内部类)

参考文章: java中的匿名内部类总结 Java中匿名类的两种实现方式 一、什么是匿名内部类 1:匿名内部类 匿名内部类也就是没有名字的内部类。 2:匿名内部类的作用 正因为没有名字...
  • w410589502
  • w410589502
  • 2017-02-12 16:37:05
  • 943

java中的nested-class和inner-class

参考了这篇文章: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html nested-class和inner-c...
  • OnlyQi
  • OnlyQi
  • 2013-01-28 13:49:30
  • 1252

关于java内部类(Inner Class) 不断更新中

java内部类(Inner Class) Inner Class 即嵌套类,也即C++和C#中的Nested Class。但Java 的Inner Class 与 C++和C#最大的不同之处在于,嵌套...
  • coolwzjcool
  • coolwzjcool
  • 2007-04-19 15:10:00
  • 1450

内部类(Inner Class)及匿名类(Anonymous Class)

内部类(Inner Class)及匿名类(Anonymous Class)。简单地说,内部类是定义在其他类中的类,内部类的主要作用是将逻辑上相关联的类放到一起;而匿名类是一种特殊的内部类,它没有类名,...
  • haobo920
  • haobo920
  • 2010-07-30 20:08:00
  • 3314

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

Java的内部类(Inner Class)和嵌套类(Nested Class)的区别

1. 静态的内部类称为嵌套类,嵌套类
  • Holmes_Conan
  • Holmes_Conan
  • 2014-11-16 21:52:00
  • 1160

【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:58
  • 2881

Static Nested Class 和Inner Class的不同

Static nested class(嵌套类)是将内部类声明为static.普通内部类对象隐式地保存了一个引用,指向创建它的外围类对象,不能有static数据和static字段。嵌套类意味着:1> ...
  • owenliu563
  • owenliu563
  • 2014-03-08 13:14:38
  • 1296

Java学习--Interface, inner class, anonymous inner class, lambda实例比较

通过接口,内部类,匿名内部类,lambda表达式实现一个相同的例子,该例子输出每隔一秒钟输出一条打印语句,同时弹出一个对话窗口,点击OK键,则程序退出。 Interface TimerTest.j...
  • WAN_EXE
  • WAN_EXE
  • 2016-11-01 08:28:51
  • 819
收藏助手
不良信息举报
您举报文章:java inner class
举报原因:
原因补充:

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