【问题标题】:error : *void is not public in *class; cannot be accessed error from outside package错误:*void 在 *class 中不公开;无法从外部包访问错误
【发布时间】:2015-06-19 10:38:11
【问题描述】:

我有一个名为 Ullman 的 jar 库。这个 jar 包含 1 个名为 ullman 的类。我试图从该类访问一个 void,当我刚刚运行程序时,它正在工作,但是当我尝试 Clean and Build 它时,我收到以下错误:

error: match(ArrayList<int[][]>,int[][],ArrayList<ArrayList<String>>,ArrayList<String>,ArrayList<Integer>) is not public in Ullman; cannot be accessed from outside package
    u.match(matrixgraph, matrixq, nodegraph, nodequery, nocandidategraph);

这是我导入该类的代码:

import ullman.Ullman;
public class Gui extends javax.swing.JFrame {
   int max_frag;
   int ratio;
   public Ullman u=new Ullman();
........

然后当我尝试从此 GUI 类访问 void 时出现错误 调用void时的示例代码

u=new Ullman();
u.match(matrixgraph, matrixq, nodegraph, nodequery, nocandidategraph);

我该如何解决?

以下是ullman类中的match方法:

public void match(ArrayList<int[][]> matrixgraph, int[][] matrixquery, ArrayList<ArrayList<String>> nodegraph, ArrayList<String> nodequery, ArrayList<Integer> nocandidate) {
    answer.clear();
    matrixq=matrixquery;
    for (int i = 0; i < matrixgraph.size(); i++) {
        int [][]matrixsmile=matrixgraph.get(i);
        matrixg=new int[matrixsmile.length-1][matrixsmile.length-1];
        for(int x=0;x<matrixg.length;x++){
            for(int y=0;y<matrixg.length;y++){
                matrixg[x][y]=matrixsmile[x][y];
            }
        }
        matrix_query_graph = new int[matrixq.length][matrixg[0].length];
        for (int j = 0; j < matrix_query_graph.length; j++) {
            for (int k = 0; k < matrix_query_graph[0].length; k++) {
                matrix_query_graph[j][k] = 0;
            }
        }

        // adjacency matrix M
        ArrayList<String>nodeq = nodequery;
        ArrayList<String>nodeg = nodegraph.get(i);
        for(int m=0;m<nodeq.size();m++){
            for(int n=0;n<nodeg.size();n++){
                if (nodeq.get(m).equals(nodeg.get(n))) {
                    matrix_query_graph[m][n] = 1;
                }
            }
        }
        if (subgraphMatching(matrixq, matrixg, matrix_query_graph)) {
            answer.add(nocandidate.get(i));
        }
    }
}

【问题讨论】:

  • 请张贴match()的方法签名。另外,您的Ullman 课程是公开的吗?如果没有,它不能在它自己的包之外使用。
  • 它已经在那里了。是的,厄尔曼课程已经公开了。现在我怀疑它无法在 jar 库中的其他包的类中访问该方法。但为什么?我很困惑,为什么我不能从 jar 库中的包中的类访问方法,但我可以从 1 个项目中不同包中的其他类访问方法(不是 jar 库)。
  • 修复了格式、语法和拼写错误以提高可读性。请尝试改进标题并添加更多详细信息。

标签: java jar package


【解决方案1】:

您的错误是说您尝试访问的方法在您导入的类中不是公开的。

error: match(ArrayList<int[][]>,int[][],ArrayList<ArrayList<String>>,ArrayList<String>,ArrayList<Integer>) is not public in Ullman; cannot be accessed from outside package
    u.match(matrixgraph, matrixq, nodegraph, nodequery, nocandidategraph);

错误明确表示无法在 Ullman 包外访问方法

因此,将Ullman 包中的match() 方法公开,然后您就可以访问它了。

【讨论】:

  • 如何公开?我只是尝试在 void 前面添加单词“public”,但它也不起作用
【解决方案2】:

您看到的输出表明Ullman.match()private,您无法直接从示例代码中访问它。具体来说,match()方法是package private,也就是说它只能被Ullman的子类调用在同一个包中的其他类。 p>

如果您有权访问 Ullman 课程,请尝试将 match() 公开。如果您没有可以访问源代码并且您必须直接访问此方法,那么您可以尝试子类化Ullman,然后添加一个调用@987654328 的辅助方法@像这样:

public class NewUllman extends Ullman {
    public void callMatch(ArrayList<int[][]> p1,int[][] p2, ArrayList<ArrayList<String>> p3, ArrayList<String> p4, ArrayList<Integer> p5) {
        match(p1, p2, p3, p4, p5);
    }
}

【讨论】:

  • 如果我可以访问 ullman 类,如何将 void match() 公开?我也尝试在它前面添加“public”,但无法正常工作
  • ullman 的这个子类化是否必须在 Gui 包中构建?我试了一下,但它也显示错误“错误:找不到符号匹配(p1,p2,p3,p4,p5);符号:方法匹配(ArrayList,int[][],ArrayList>,ArrayList,ArrayList) 位置:类 SubgraphIsomorphismTest
  • but can't work ...这对我们没有帮助。请清楚说明发生了什么。
  • 我尝试在“void match”前面添加“public”,但仍然出现错误“无法从外部包访问错误”
  • 这听起来好像您在尝试使用更新的方法时没有包含修改后的代码。您确定您的修改将其放入用于构建您的项目的 JAR 中吗?
猜你喜欢
  • 2018-06-27
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多