【问题标题】:How to call a method with parameter from a diffrent class如何使用来自不同类的参数调用方法
【发布时间】:2014-11-27 16:43:37
【问题描述】:

可能的菜鸟问题,但我不能让我的方法在一个类中调用另一个类?

头等舱

public class Firstclass {

    public static void main(String[] args) {
        Test1 test = new Test1();
        test.Passingvalue();
        test.myMethod();

    }
}

二等

import java.util.Scanner;

public class Test1 {

    public void Passingvalue (){
        Scanner Scan = new Scanner(System.in);
        System.out.println("File Name ? ");
        String txtFile = Scan.next();
    }

    public void myMethod(String txtFile){

        System.out.print("Scan this file" + txtFile);

    }
}

【问题讨论】:

  • 您没有向test.myMethod 发送任何参数。那么它应该如何工作,也许是魔法?
  • 这就是我想问的问题
  • 你可以检查你发送到System.out.println(...)的参数作为例子。
  • 是的,我知道参数没问题,但在 test.myMethod 上出现错误,因为我猜你需要括号之间的东西,但我不知道括号之间需要什么,因为如果我只是名字方法中的参数会出错
  • 我得到一个错误,因为我猜你需要括号之间的东西,但我不知道括号之间需要什么该方法在其签名中声明:String txtFile,所以它需要一个String 作为参数。您可以通过使用 "look! this is a String! Yay!" 或将文字 String 分配给 String 变量来创建文字 String,例如String foo = "this is foo";,然后将这个 String(字面量或从变量存储)传递给您的方法。

标签: java methods parameter-passing


【解决方案1】:

您可以在方法名称后的括号中以逗号分隔列表的形式提供参数:

public static void main(String[] args) {
    Test1 test = new Test1();

    test.myMethod("my_file.txt");
}

【讨论】:

    【解决方案2】:

    不要忘记添加这样的参数:

    test.myMethod("txtFile");
    

    【讨论】:

      【解决方案3】:

      将您的字符串 txtfile 声明为两个方法之外的公共静态变量(在类 test1 的开头)。

      public class Firstclass {
      
      public static void main(String[] args) {
          Test1 test = new Test1();
          test.Passingvalue();
          test.myMethod();
      
      }
      

      }

      导入 java.util.Scanner;

      公共类测试1 {

      String txtFile;
      
      public void Passingvalue (){
          Scanner Scan = new Scanner(System.in);
          System.out.println("File Name ? ");
          txtFile = Scan.next();
      }
      
      public void myMethod(){
      
          System.out.print("Scan this file" + txtFile);
      
      }
      

      }

      【讨论】:

        【解决方案4】:

        我认为你在这里有一个误解:

        public void Passingvalue (){
            Scanner Scan = new Scanner(System.in);
            System.out.println("File Name ? ");
            String txtFile = Scan.next(); //method scope only
        }
        

        这里的局部变量txtFile 仅在方法Passingvalue(顺便检查命名约定)完成之前存在,即它具有方法范围。因此,当调用myMethod(String txtFile) 时,参数具有相同的名称,但在不同的范围内是不同的引用。

        因此,您要么必须像其他人已经建议的那样将文件名传递给您的方法,要么更改 txtFile 的范围,例如使其成为实例变量:

        public class Test1 {
          private String txtFile; //the scope of this variable is the instance, i.e. it exists as long as the instance of Test1 exists.
        
          public void Passingvalue (){
            Scanner Scan = new Scanner(System.in);
            System.out.println("File Name ? ");
            txtFile = Scan.next();
          }
        
          public void myMethod(){
            System.out.print("Scan this file" + txtFile);
          }
        }
        

        请注意,这只是为了说明眼前的问题。还有其他问题,例如与一般设计,没有解决。无论如何,您的代码的目的似乎是学习,所以设计现在不是什么大问题。

        作为一个提示:我可能会从方法外部传递名称或在构造函数中传递/读取它。

        【讨论】:

        • 谢谢你这是我一直在努力做的。
        【解决方案5】:

        当您调用参数化方法时,您应该将参数传递给调用方法,否则 jvm 将无法理解您正在调用谁的方法,因为基于参数我们可以重载方法。

        所以你的问题的最终答案是

        public static void main(String[] args) {
            Test1 test = new Test1();
        
            test.myMethod("place your file name here");
        }
        

        【讨论】:

        • 好吧,我想我明白了,你就是不能按照我尝试的方式去做
        猜你喜欢
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 2022-01-07
        • 2020-08-27
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多