Java 处理 class 类型参数的方式

发布于 2024-12-04 21:20:29 字数 420 浏览 1 评论 0原文

我正在查看 Walter Savitch 书中的示例“Java 简介”

// This method does not do what we want it to do.
public static void openFile(String fileName,
    PrintWriter stream) throws FileNotFoundException
{
    stream = new PrintWriter(fileName);
}

PrintWriter toFile = null;
try
{
    openFile("data.txt", toFile);
}

作者给出的解释,这没有任何意义,至于为什么在 try 之后 toFile = null

谢谢!

I am looking at example in the example in Walter Savitch book "Java Introduction"

// This method does not do what we want it to do.
public static void openFile(String fileName,
    PrintWriter stream) throws FileNotFoundException
{
    stream = new PrintWriter(fileName);
}

PrintWriter toFile = null;
try
{
    openFile("data.txt", toFile);
}

Author gives explanation, which does not make any sense, as to why toFile = null after try

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-12-11 21:20:29

您需要更深入地阅读本书的该部分。 (我确信它确实有意义......但你只是还没有理解它。)

很明显,作者实际上是在试图说明 Java 的一个非常重要的方面......参数是如何传递的。

具体来说,他试图说明两个stream标识符用于不同变量,并且方法内的赋值

stream = new PrintWriter(fileName);

不会影响streamtry 之前声明的变量。分配给方法内 stream 变量的值会丢失。

这说明Java使用“按值传递”作为其参数传递机制。 (如果您需要从方法调用中获取值,最简单的方法就是返回它。)

You need to read that section of the book more deeply. (I'm sure that it actually DOES make sense ... but you just haven't understood it yet.)

It is clear that the author is actually trying to illustrate an very important aspect of Java ... how parameters are passed.

Specifically, he is trying to illustrate that the two stream identifiers are for different variables, and that the assignment inside the method

stream = new PrintWriter(fileName);

does NOT effect the stream variable declared just before the try. The value that is assigned to the stream variable inside the method gets lost.

This illustrates that Java uses "pass by value" as its parameter passing mechanism. (If you need to get a value back from a method call, the simple way to do it is to return it.)

诗笺 2024-12-11 21:20:29

作者试图向您解释,更改方法内部变量所指的内容在方法外部是不可见的。

调用 openFile() 后,toFile 将为 null,因为引用值已传递给该方法并分配给本地 流变量。除非显式返回,否则在方法外部无法看到对方法内部 stream 值的更改。

openFile() 需要返回新的 PrintWriter:

public static PrintWriter openFile(String fileName) throws FileNotFoundException
{
    PrintWriter stream = new PrintWriter(fileName);
    return stream;
}

并被调用为:

PrintWriter toFile = null;
try
{
    toFile = openFile("data.txt");
}

The author is trying to explain to you that changing what a variable refers to inside a method isn't visible outside the method.

toFile will be null after the call to openFile() because the reference value is passed to the method and assigned to the local stream variable. Changing the value of stream inside the method can't be seen outside the method unless you explicitly return it.

openFile() would need to return the new PrintWriter:

public static PrintWriter openFile(String fileName) throws FileNotFoundException
{
    PrintWriter stream = new PrintWriter(fileName);
    return stream;
}

and be called as:

PrintWriter toFile = null;
try
{
    toFile = openFile("data.txt");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文