超市采购管理系统的设计与实现外文翻译资料

 2022-11-19 16:40:45

5.1.2 Custom Tool Library

With the above knowledge in hand, you can then start creating your own tool library to reduce or eliminate duplicate code completely. For example, you can create an alias for System.out.println() to reduce the amount of code that is repeatedly typed. It can be part of a package called tools:

//: P.java

126

// The P.rint amp; P.rintln shorthand package com.bruceeckel.tools;

Public class P {

Public static void rint(Object obj) {

System.out.print(obj);

}

Public static void rint(String s) { System.out.print(s);

}

Public static void rint(char[] s) {

System.out.print(s);

}

Public static void rint(char c) {

System.out.print(c);

}

Public static void rint(int i) { System.out.print(i);

}

Public static void rint(long l) {

System.out.print(l);

}

Public static void rint(float f) {

System.out.print(f);

}

Public static void rint(double d) {

System.out.print(d);

}

Public static void rint(boolean b) {

System.out.print(b);

}

Public static void rintln() { System.out.println();

}

Public static void rintln(Object obj) {

System.out.println(obj);

}

Public static void rintln(String s) { System.out.println(s);

}

Public static void rintln(char[] s) { System.out.println(s);

}

Public static void rintln(char c) {

System.out.println(c);

}

Public static void rintln(int i) {

System.out.println(i);

}

Public static void rintln(long l) {

System.out.println(l);

}

127

Public static void rintln(float f) { System.out.println(f);

}

Public static void rintln(double d) { System.out.println(d);

}

Public static void rintln (boolean b) {

System.out.println(b);

}

} ///:~

All of the different data types can now be output on a new line (P.rintln()) or not on a new line (P.rint() ). You may guess that the directory where this file is located must start at a CLASSPATH and continue with com/bruceeckel/tools. After the compilation, using an import statement, you can use P.class files anywhere in your system. As follows:

//: ToolTest.java

// Uses the tools library import com.bruceeckel.tools.*;

Public class ToolTest {

Public static void main(String[] args) { P.rintln('Available from now on!');

}

} ///:~

So from now on, whenever you make a useful new tool, you can add it to the tools directory (or your own personal

Util or tools directory).

1. The trap of the CLASSPATH

There is a very interesting pitfall in the P.java file. Especially for early Java implementations, the correct setting of the classpath is usually

A difficult job. When I wrote this book, I introduced the P.java file, which initially seemed to work fine. But in some situations

However, it began to break down. For a long time, I was convinced that this was a mistake in Java or something else. But in the end, I finally discovered that a program was introduced in one place (CodePackager.java in Chapter 17) that used a different class P.

Because it is used as a tool, it sometimes goes into the classpath; other times it doesnt. But as long as it enters the classpath, then if the executing program needs to look for classes in com.bruceeckel.tools, Java first discovers P in CodePackager.java. At this point, the compiler will report that a particular method was not found. This is of course very vexing, because we clearly saw this method in the previous class P, and there is no more diagnostic report that can provide us with a clue, let us know that is to find a completely different class ( Thats not even public.)

At first glance, this seems to be a bug in the compiler, but if you look at the import statement, youll see that it just says: 'It may have been found here.

P'. However, we assume that the compiler searches anywhere in its classpath, so once it finds a P, it will use it;

A 'wrong' one was found in the process and it stopped searching. This is slightly different from what we described earlier, because there are some annoying classes that are all in the package. And here there is a P not in the package, but it can still be found in the regular classpath search process. If you encounter situations like this, make sure that there is only one class for each name for each place in the classpath.

5.1.3 Use the Import to Change Behavior

One feature that Java has removed is Cs 'conditional compilation,' which allows us to change parameters and get different behavior without changing any other code. Javas abandonment of this feature may be due to the fact that this feature is often used in C to solve cross-platform problems: different parts of the code are compiled according to the specific platform, otherwise they cannot be run on a specific platform. Since Javas design idea is to become an automatic cross-platform language, this feature is not necessary. However, there are other very valuable uses of conditional compilation. A very common use is to debug code. The debug feature can be used during development, but it is not available in the released product. Alen Holub (www.holub.com) proposed using packages to simulate the concept of conditional compilation. Based on this concept, it creates a very useful Java version of the C 'determinism mechanism.' The reason is called 'determination machine'

128

'System' is because we can say 'it should be true' or 'it should be false.' If the statement does not agree with your assertion, you can find out about the relevant situation. This kind of tool is especially useful during the debugging process.

The following class can be used for program debugging:

//: Assert.java

// Assertion tool for debugging

Package com.bruceeckel.t

剩余内容已隐藏,支付完成后下载完整资料


5.1.2 自定义工具库

掌握前述的知识后,接下来就可以开始创建自己的工具库,以便减少或者完全消除重复的代码。例如,可为 System.out.println()创建一个别名,减少重复键入的代码量。它可以是名为 tools 的一个包(package)的 一部分:

//: P.java

126

// The P.rint amp; P.rintln shorthand package com.bruceeckel.tools;

public class P {

public static void rint(Object obj) {

System.out.print(obj);

}

public static void rint(String s) { System.out.print(s);

}

public static void rint(char[] s) {

System.out.print(s);

}

public static void rint(char c) {

System.out.print(c);

}

public static void rint(int i) { System.out.print(i);

}

public static void rint(long l) {

System.out.print(l);

}

public static void rint(float f) {

System.out.print(f);

}

public static void rint(double d) {

System.out.print(d);

}

public static void rint(boolean b) {

System.out.print(b);

}

public static void rintln() { System.out.println();

}

public static void rintln(Object obj) {

System.out.println(obj);

}

public static void rintln(String s) { System.out.println(s);

}

public static void rintln(char[] s) { System.out.println(s);

}

public static void rintln(char c) {

System.out.println(c);

}

public static void rintln(int i) {

System.out.println(i);

}

public static void rintln(long l) {

System.out.println(l);

}

127

public static void rintln(float f) { System.out.println(f);

}

public static void rintln(double d) { System.out.println(d);

}

public static void rintln (boolean b) {

System.out.println(b);

}

} ///:~

所有不同的数据类型现在都可以在一个新行输出(P.rintln()),或者不在一个新行输出(P.rint() )。 大家可能会猜想这个文件所在的目录必须从某个 CLASSPATH 位置开始,然后继续 com/bruceeckel/tools。编 译完毕后,利用一个 import 语句,即可在自己系统的任何地方使用 P.class 文件。如下所示:

//: ToolTest.java

// Uses the tools library import com.bruceeckel.tools.*;

public class ToolTest {

public static void main(String[] args) { P.rintln('Available from now on!');

}

} ///:~

所以从现在开始,无论什么时候只要做出了一个有用的新工具,就可将其加入 tools 目录(或者自己的个人

util 或 tools 目录)。

1. CLASSPATH 的陷阱

P.java 文件存在一个非常有趣的陷阱。特别是对于早期的 Java 实现方案来说,类路径的正确设定通常都是

很困难的一项工作。编写这本书的时候,我引入了 P.java 文件,它最初看起来似乎工作很正常。但在某些情

况下,却开始出现中断。在很长的时间里,我都确信这是 Java 或其他什么在实现时一个错误。但最后,我终 于发现在一个地方引入了一个程序(即第 17 章要说明的 CodePackager.java),它使用了一个不同的类 P。

由于它作为一个工具使用,所以有时候会进入类路径里;另一些时候则不会这样。但只要它进入类路径,那 么假若执行的程序需要寻找 com.bruceeckel.tools 中的类,Java 首先发现的就是 CodePackager.java 中的 P。此时,编译器会报告一个特定的方法没有找到。这当然是非常令人头疼的,因为我们在前面的类 P 里明明 看到了这个方法,而且根本没有更多的诊断报告可为我们提供一条线索,让我们知道找到的是一个完全不同 的类(那甚至不是 public 的)。

乍一看来,这似乎是编译器的一个错误,但假若考察 import 语句,就会发现它只是说:“在这里可能发现了

P”。然而,我们假定的是编译器搜索自己类路径的任何地方,所以一旦它发现一个 P,就会使用它;若在搜

索过程中发现了“错误的”一个,它就会停止搜索。这与我们在前面表述的稍微有些区别,因为存在一些讨 厌的类,它们都位于包内。而这里有一个不在包内的 P,但仍可在常规的类路径搜索过程中找到。 如果您遇到象这样的情况,请务必保证对于类路径的每个地方,每个名字都仅存在一个类。

5.1.3 利用导入改变行为

Java 已取消的一种特性是 C 的“条件编译”,它允许我们改变参数,获得不同的行为,同时不改变其他任何 代码。Java 之所以抛弃了这一特性,可能是由于该特性经常在 C 里用于解决跨平台问题:代码的不同部分根 据具体的平台进行编译,否则不能在特定的平台上运行。由于 Java 的设计思想是成为一种自动跨平台的语 言,所以这种特性是没有必要的。 然而,条件编译还有另一些非常有价值的用途。一种很常见的用途就是调试代码。调试特性可在开发过程中 使用,但在发行的产品中却无此功能。Alen Holub(www.holub.com )提出了利用包(package)来模仿条件 编译的概念。根据这一概念,它创建了 C“断定机制”一个非常有用的 Java 版本。之所以叫作“断定机

128

制”,是由于我们可以说“它应该为真”或者“它应该为假”。如果语句不同意你的断定,就可以发现相关 的情况。这种工具在调试过程中是特别有用的。

可用下面这个类进行程序调试:

//: Assert.java

// Assertion tool for debugging

package com.bruceeckel.tools.debug;

public class Assert {

private static void perr(String msg) { System.err.println(msg);

}

public final static void is_true(boolean exp) {

if(!exp) perr('Assertion failed' );

}

public final static void is_false(boolean exp){

if(exp) perr('Assertion failed');

}

public final static void

is_true(boolean exp, String msg) {

if(!exp) perr('Assertion failed: ' msg);

}

public final static void

is_false(bo olean exp, String msg) {

if(exp) perr('Assertion failed: ' msg);

}

} ///:~

这个类只是简单地封装了布尔测试。如果失败,就显示出出错消息。在第 9 章,大家还会学习一个更高级的 错误控制工具,名为“违例控制”。但在目前这种情况下,perr()方法已经可以很好地工作。 如果想使用这个类,可在自己的程序中加入下面这一行:

import com.bruceeckel.tools.debug.*; 如欲清除断定机制,以便自己能发行最终的代码,我们创建了第二个 Assert 类,但却是在一个不同的包里:

//: Assert.java

// Turning off the assertion output

// so you can ship the program.

package com.bruceeckel.tools;

public class Assert {

public final static void is_true(boolean exp){} public final static void is_false(boolean exp){} public final static void

is_true(boolean exp, String msg) {} public final static void is_false(boolean exp, String msg) {}

} ///:~

现在,假如将前一个 import 语句变成下面这个样子: import com.bruceeckel.tools.*; 程序便不再显示出断言。下面是个例子:

129

//: TestAssert.jav a

// Demonstrating the assertion tool

package c05;

// Comment the following, and uncomment the

// subsequent line to change assertion behavior:

import com.bruceeckel.tools.debug.*;

// import com.bruceeckel.tools.*;

public class TestAssert {

public static void main(String[] args) {

Assert.is_true((2 2) == 5);

Assert.is_false((1 1) == 2); Assert.is_true((2 2) == 5, '2 2 == 5');

Assert.is_false((1 1) == 2, '1 1 != 2');

}

} ///:~

通过改变导入的 package,我们可将自己的代码从调试版本变成最终的发行版本。这种技术可应用于任何种 类的条件代码。

5.1.4 包的停用 大家应注意这样一个问题:每次创建一个包后,都在为包取名时间接地指定了一个目录结构。这个包必须存 在(驻留)于由它的名字规定的目录内。而且这个目录必须能从 CLASSPATH 开始搜索并发现。最开始的时

候,package 关键字的运用可能会令人迷惑,因为除非坚持遵守根据目录路径指定包名的规则,否则就会在

运行期获得大量莫名其妙的消息,指出找不到一个特定的类——即使那个类明明就在相同的目录中。若得到 象这样的一条消息,请试着将 package 语句作为注释标记出去。如果这样做行得通,就可知道问题到底出在

哪儿。

5.2 Java 访问指示符

针对类内每个成员的每个定义

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[23376],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

课题毕业论文、外文翻译、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。