gpt4 book ai didi

java - 使用文件作为参数调用构造函数

转载 作者:行者123 更新时间:2023-12-02 09:33:25 28 4
gpt4 key购买 nike

我有一个类 SpellingSuggestor,其构造函数具有签名

<小时/>

public SpellingSuggestor(文件文件)抛出 IOException {//某事 }

我想从另一个类调用它的构造函数。代码是这样的

public class NLPApplications
public static void main(String[] args) {
String w= "randomword";
URL url = getClass().getResource("big.txt");
File file = new File(url.getPath());

System.out.println((new SpellingSuggestor(file)).correct(w));
}
}

但上面的 URL url.. 行显示错误

  1. URL 无法解析为类型。
  2. 无法从 Object 类型对非静态方法 getClass() 进行静态引用。

出了什么问题?

<小时/>

我看了这个问题How to pass a text file as a argument? 。我不习惯用 Java 处理文件,所以这个问题。

最佳答案

getclass() 是一个非静态方法,您不能从静态主方法中引用。

为什么会这样? find here it is already answered通过 danben

解决方法是 -

NLPApplications.class.getClass().getResource("big.txt");

关于java - 使用文件作为参数调用构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271856/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com