Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
无名包中的类
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post will appear after it is approved by moderators
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stephen Suen  
View profile   Translate to Translated (View Original)
 More options May 10 2006, 11:50 am
From: "Stephen Suen" <stephen.s...@gmail.com>
Date: Wed, 10 May 2006 23:50:26 +0800
Local: Wed, May 10 2006 11:50 am
Subject: [TechTips] 无名包中的类

为了便于开发小型的或者临时性的程序,Java 语言中允许所谓的无名包(unnamed
package)。实践中,无名包经常用于刚起步的开发中,或者作为范例的程序中。在语言规范的 7.4.2 Unnamed
Packages<http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.h...>一节可以找到对无名包的定义。

尽管无名包在某些情况下的确是有其方便之处。不过,一个必须注意的问题是:在无名包中的类无法被其它包中类所引用。换句话说,其它包中的类无法引入无名包中的类 。参考以下链接可以了解关于这个限制进一步的内容:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4361575

在语言规范的第二版中,与无名包的这个问题相关的规范说明并不是十分严谨,从语言规范中找出这一限制的可靠依据并非易事。不过,6.5.5.1 Simple
Type Names<http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html...>
 一节的第一段的说明,可以作为这一限制的根源之一。摘录如下:

If a type name consists of a single *Identifier*, then the identifier must
occur in the scope of a declaration of a type with this name, or a
compile-time error occurs.

而在无名包中声明的类型的范围仅限于无名包以内,这意味着其它包中的类型不能通过简单名称直接引用无名包中的类型。

正如上面的 bug 描述所言,在 JDK 1.4 以前,可以通过 import TypeName 的方式直接引入无名包中的类型。不过,JDK
1.4之后就不允许这种引入了;语言规范在第三版中更是直截了当地说明了这一限制,参考 7.5
Import Declarations<http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5>
 一节的说明。

无名包的这一限制通常不会造成什么问题。不过有时问题可能由于复杂的应用环境而变得不易发觉。比如,在开发 JSP 应用时,使用 <jsp:useBean>
使用的 Java Bean 的实现或者自定义标签(custom tag)的标签处理器(tag handler)的实现是在无名包中时,很有可能在编译
JSP 时报告找不到所需的类。这是因为 JSP 的编译器多半不会将 JSP 文件编译为无名包的 JSP 实现类(比如 Tomcat 的 JSP
实现类的包为 org.apache.jsp 或其子包)。

--
Stephen Suen
stephen.s...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google