【发布时间】: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