gpt4 book ai didi

java - 我可以从导入的包中为类创建对象吗

转载 作者:行者123 更新时间:2023-12-01 18:02:51 25 4
gpt4 key购买 nike

我是java初学者。

我在代码中导入了一个包,并且想要使用导入类中的函数。它说对象无法解析。

代码如下

import edu.princeton.cs.algs4.WeightedQuickUnionUF;

public class Percolation {
private int[] full;
private int length;
private int size;

public Percolation(int n) {
length = n + 2;
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(length);

}

// Now if I use the uf object in another function as below

public boolean isFull(int i, int j) {
boolean result = false;
if(uf.connected(0,i+j)) {
result = true;
}
return result;
}
}

//uf.connected in the public function declared in WeightedQuickUnionUF package.
//Its definition is as follows
public boolean connected(int p, int q) { /* ... */ }

它给了我一个错误

Error: uf cannot be resolved

uf.connectedWeightedQuickUnionUF 包中声明的公共(public)函数中。其定义如下

public boolean connected(int p, int q) { /* ... */ }

请告知如何从导入的包中访问该功能。提前致谢!

最佳答案

您已在构造函数中创建了局部变量 uf 并尝试从方法 isFull() 访问它。

您可以:

  1. 在方法 isFull() 中创建对象 uf 并使用它
  2. 使 uf 成为您的类的成员并使用它。
  3. 使用构造函数中的 uf 调用方法。

关于java - 我可以从导入的包中为类创建对象吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39356812/

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