【问题标题】:use method of one class in another class of different package在不同包的另一类中使用一个类的方法
【发布时间】:2017-11-10 14:04:29
【问题描述】:

我有以下代码:

public class class1 {
.............

public static void calls(String[] args) {

    ................

现在我试图在另一个方法中的另一个类中调用“class1”的这个方法“调用”,如下代码:

public class newClass extends abc {

public void executemywork() throws CIException {

class1.calls(args); //error here "args" cannot be resolved to a variable

我尝试过查看堆栈溢出的其他解决方案。我什至尝试通过创建“class1”对象并在第二种方法中使用它。我在这里仍然遇到同样的错误。错误说“args”无法解析为变量。这两个类都在不同的包中,我也导入了另一个类。

【问题讨论】:

  • 通话没问题。问题是,正如错误消息所说,您尚未定义 args,这是您要传递的内容 calls
  • 你没有在 newClass 类中声明你的变量 args
  • 该错误与方法的位置无关。你根本没有任何名为args的东西。
  • 这是重复的,可能是stackoverflow.com/questions/7588784/…,但我还在寻找......
  • Si 如果我初始化为 String[] args = null;这应该做的工作

标签: java


【解决方案1】:

声明 args 并为其赋值

String[] args = new String[]{"a","b","c"};

【讨论】:

    【解决方案2】:

    它是compilation error 你正在做 class1.calls(args); 但这里 args 没有定义。因此编译错误。

    更改您的代码,例如

    public class newClass extends abc {
    
                public void executemywork() throws CIException {
                        String[] args = new String[]{"a","b","c"};
                        class1.calls(args); //error here "args" cannot be resolved to a variable
                }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多