如何在运行时更改另一个文件时识别我的代码?

发布于 2025-02-01 08:44:26 字数 1436 浏览 1 评论 0原文

我在一个文件xfunction.java中有一个方法,它在另一个文件中重写了另一个文件ndrive.java,然后调用NDRIVE类重写的方法。问题是,当调用该方法时,我的代码似乎仍然认为该方法是在重写之前。实际上,即使ndrive.delete()返回true和ndrive.exists()返回false,我的代码仍然读取旧方法(并且文件ndrive.java仍然存在)。我的方法没有任何意义吗?它是从根本上有缺陷的,还是我可以修复它?

这是下面的方法。

    private double evaluate(String exp) {

    File ndrive = new File("NDrive.java");
    PrintWriter write = null;
    Scanner scan = null;

    try {

        scan = new Scanner(ndrive);

    } catch (FileNotFoundException fnfe) {

        System.out.println("File not found");

    }

    int lineCount = 0;
    String contents = "";
    if (scan != null) {
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            lineCount++;
            if (lineCount == 9) {
                contents += "        double y = " + exp + ";\n";
            } else {
                contents += line + "\n";
            }
        }

        
        try {
            write = new PrintWriter(ndrive);
        } catch (FileNotFoundException fnfe) {
            System.out.println("File not found for writer");
        }

        write.print(contents);
        System.out.print(contents);

        write.close();
        scan.close();


    }

    return NDrive.returnY();
}

ndrive.returny()线应返回不同的输出,因为该方法的主体(内容)在ndrive.java中被更改。但事实并非如此。除非返回()的主体被“更改”到它已经发生的(不变),否则在我不更改输入exp的情况下再次运行程序后发生,因为文件ndrive.java已经包含了新行。

谁能帮忙?我一直在看这个非常非常长的时间。

I have a method in one file, XFunction.java, that rewrites another class in another file, NDrive.java, then calls the rewritten method from the NDrive class. The problem is, when the method is called, my code still seems to think the method is what it was before it was rewritten. In fact, even if ndrive.delete() returns true and ndrive.exists() returns false, my code STILL reads the old method (and the file NDrive.java is still there). Does my approach not make any sense? Is it fundamentally flawed, or can I fix it?

Here's the method below.

    private double evaluate(String exp) {

    File ndrive = new File("NDrive.java");
    PrintWriter write = null;
    Scanner scan = null;

    try {

        scan = new Scanner(ndrive);

    } catch (FileNotFoundException fnfe) {

        System.out.println("File not found");

    }

    int lineCount = 0;
    String contents = "";
    if (scan != null) {
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            lineCount++;
            if (lineCount == 9) {
                contents += "        double y = " + exp + ";\n";
            } else {
                contents += line + "\n";
            }
        }

        
        try {
            write = new PrintWriter(ndrive);
        } catch (FileNotFoundException fnfe) {
            System.out.println("File not found for writer");
        }

        write.print(contents);
        System.out.print(contents);

        write.close();
        scan.close();


    }

    return NDrive.returnY();
}

The line NDrive.returnY() should return a different output since the body (contents) of that method in Ndrive.java was changed. But it doesn't. Unless the body of returnY() is "changed" to what it already is (is unchanged) which happens after I run the program again without changing the input exp, because the file NDrive.java already contains the new line.

Can anyone help? I've been looking at this for a really, really long time.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文