The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Questions and Exercises: Creating and Using Packages问题和练习:创建和使用包
Questions问题
Assume you have written some classes. Belatedly, you decide they should be split into three packages, as listed in the following table. 假设您已经编写了一些类。后来,您决定将它们分为三个包,如下表所示。Furthermore, assume the classes are currently in the default package (they have no package
statements).此外,假设这些类当前在默认包中(它们没有package
语句)。
Destination Packages目的地包裹
Package Name包名 |
Class Name类名 |
mygame.server |
Server |
mygame.shared |
Utilities |
mygame.client |
Client |
Which line of code will you need to add to each source file to put each class in the right package?您需要向每个源文件添加哪行代码才能将每个类放入正确的包中?
To adhere to the directory structure, you will need to create some subdirectories in the development directory and put source files in the correct subdirectories. 为了遵循目录结构,您需要在开发目录中创建一些子目录,并将源文件放在正确的子目录中。What subdirectories must you create? 您必须创建哪些子目录?Which subdirectory does each source file go in?每个源文件放在哪个子目录中?
Do you think you'll need to make any other changes to the source files to make them compile correctly? If so, what?您认为需要对源文件进行任何其他更改以使其正确编译吗?如果是,什么?
Exercises练习
Download the source files as listed here.下载此处列出的源文件。
Implement the changes you proposed in questions 1 through 3 using the source files you just downloaded.使用刚下载的源文件实施问题1至3中提出的更改。
Compile the revised source files. 编译修改后的源文件。(Hint: If you're invoking the compiler from the command line (as opposed to using a builder), invoke the compiler from the directory that contains the mygame
directory you just created.)(提示:如果您是从命令行调用编译器(而不是使用生成器),请从包含您刚刚创建的mygame
目录的目录中调用编译器。)
Check your answers.检查你的答案。