Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Learning the Java Language
Lesson: Packages

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
  1. Which line of code will you need to add to each source file to put each class in the right package?您需要向每个源文件添加哪行代码才能将每个类放入正确的包中?
  2. 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?每个源文件放在哪个子目录中?
  3. 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.下载此处列出的源文件。

  1. Implement the changes you proposed in questions 1 through 3 using the source files you just downloaded.使用刚下载的源文件实施问题1至3中提出的更改。
  2. 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.检查你的答案。

Previous page: Summary of Creating and Using Packages
Next page: End of Trail