一个《TIJ3》的问题,来看看

RookieStar 2004-03-22 08:00:19
小弟我最近开始学JAVA,看的是著名的TIJ 3rd E文版。由于有C++的基础,看起来相当爽的。但今天看CHAPTER 8的Inner Class时,被一个术语所困惑:p335,para2,line10中的“type-coding dependencies”,我从字面理解是“对具体类型编程方式的依赖”,可惜不能明白为何Eckel在这儿要说这句话(没从前面的example看出type-coding dependencies来),请前辈们予以指点,或者给一个更具体的例子来说明。

(附原文p333-p335)
Inner classes and upcasting

So far, inner classes don’t seem that dramatic. After all, if it’s hiding you’re after, Java already has a perfectly good hiding mechanism—just give the class package access (visible only within a package) rather than creating it as an inner class.

However, inner classes really come into their own when you start upcasting to a base class, and in particular to an interface. (The effect of producing an interface reference from an object that implements it is essentially the same as upcasting to a base class.) That’s because the inner class—the implementation of the interface—can then be completely unseen and unavailable to anyone, which is convenient for hiding the implementation. All you get back is a reference to the base class or the interface.

First, the common interfaces will be defined in their own files so they can be used in all the examples:
//: c08:Destination.java
public interface Destination {
String readLabel();
} ///:~

//: c08:Contents.java
public interface Contents {
int value();
} ///:~

Now Contents and Destination represent interfaces available to the client programmer. (The interface, remember, automatically makes all of its members public.)

When you get back a reference to the base class or the interface, it’s possible that you can’t even find out the exact type, as shown here:

//: c08:TestParcel.java
// Returning a reference to an inner class.

class Parcel3 {
private class PContents implements Contents {
private int i = 11;
public int value() { return i; }
}
protected class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
public Destination dest(String s) {
return new PDestination(s);
}
public Contents cont() {
return new PContents();
}
}

public class TestParcel {
public static void main(String[] args) {
Parcel3 p = new Parcel3();
Contents c = p.cont();
Destination d = p.dest("Tanzania");
// Illegal -- can't access private class:
//! Parcel3.PContents pc = p.new PContents();
}
} ///:~

In the example, main( ) must be in a separate class in order to demonstrate the privateness of the inner class PContents.

In Parcel3, something new has been added: The inner class PContents is private, so no one but Parcel3 can access it. PDestination is protected, so no one but Parcel3, classes in the same package (since protected also gives package access), and the inheritors of Parcel3 can access PDestination. This means that the client programmer has restricted knowledge and access to these members. In fact, you can’t even downcast to a private inner class (or a protected inner class unless you’re an inheritor), because you can’t access the name, as you can see in class TestParcel. 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.
...全文
45 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
RookieStar 2004-03-22
  • 打赏
  • 举报
回复
是不是class designer利用private inner class来防止client programmer对这个类实现upcasting(polymorphism)? 但好像这样意义不大啊。
是不是上面那个example并没有显示出“preventing type-coding dependencies”,我觉得很抽象啊!
forgetheart 2004-03-22
  • 打赏
  • 举报
回复
意思是“与型别相依的程序代码”

在你不知道具体的型别时,也可以调用某个接口或抽象类的具体实现。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧