Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将变量传递给不同的方法_Java - Fatal编程技术网

Java 如何将变量传递给不同的方法

Java 如何将变量传递给不同的方法,java,Java,如何将tom.name、id age和year变量从main方法传递到“tomdetails”方法中,以便该方法能够识别它们 class Student { int id; int age; int year; String name; } class Staff { int id; int age; String name; String postcode; String department; } public cl

如何将tom.name、id age和year变量从main方法传递到“tomdetails”方法中,以便该方法能够识别它们

class Student {
    int id;
    int age;
    int year;
    String name;
}

class Staff {
    int id;
    int age;
    String name;
    String postcode;
    String department;
}

public class Main {

    public static void main(String[] args) {
        //Database
        //Students
        Student tom = new Student();
        tom.name = "Tom";
        tom.id = 1;
        tom.age = 15;
        tom.year = 10;

       }

    private static void tom_details() {
        System.out.println(tom.name);
        System.out.println(tom.id);
        System.out.println(tom.age);
        System.out.println(tom.year);
    }
}

我想你可以只传递对象
tom
: 将方法更改为

    private static void tom_details(Student tom) {
        System.out.println(tom.name);
        System.out.println(tom.id);
        System.out.println(tom.age);
        System.out.println(tom.year);
    }

我想你可以只传递对象
tom
: 将方法更改为

    private static void tom_details(Student tom) {
        System.out.println(tom.name);
        System.out.println(tom.id);
        System.out.println(tom.age);
        System.out.println(tom.year);
    }
虽然可以单独传递变量,但将引用传递给整个
Student
对象可能更有意义。例如:

publicstaticvoidmain(字符串[]args){
学生汤姆=新生();
tom.name=“tom”;
tom.id=1;
汤姆:年龄=15岁;
汤姆。年=10;
印刷细节(tom);
}
私人静态无效打印详细信息(学生){
System.out.println(学生姓名);
System.out.println(学生id);
系统输出打印LN(学生年龄);
系统输出打印LN(学生年);
}
之后,我将采取以下步骤:

  • Student
    一个接受姓名、ID、年龄和年份的构造函数
  • Student
    中的所有字段设置为私有(可能是最终字段),而不是通过方法(例如
    getName()
    )公开数据
  • 可以在
    Student
    中添加
    printDetails()
    方法,这样您就可以在
    main
    方法中调用
    tom.printDetails()
虽然可以分别传递变量,但将引用传递给整个
Student
对象可能更有意义。例如:

publicstaticvoidmain(字符串[]args){
学生汤姆=新生();
tom.name=“tom”;
tom.id=1;
汤姆:年龄=15岁;
汤姆。年=10;
印刷细节(tom);
}
私人静态无效打印详细信息(学生){
System.out.println(学生姓名);
System.out.println(学生id);
系统输出打印LN(学生年龄);
系统输出打印LN(学生年);
}
之后,我将采取以下步骤:

  • Student
    一个接受姓名、ID、年龄和年份的构造函数
  • Student
    中的所有字段设置为私有(可能是最终字段),而不是通过方法(例如
    getName()
    )公开数据
  • 可以在
    Student
    中添加
    printDetails()
    方法,这样您就可以在
    main
    方法中调用
    tom.printDetails()

我建议阅读有关方法的教程,例如,对象“tom”的可能副本是main()方法的本地副本。此对象只能在main()方法中访问。因此,您无法访问作用域之外的本地成员,即tom_details()方法内部。此问题有两种解决方案。1) 将“tom”对象传递给tom_details()方法。2) 使用“static”关键字在主类中全局声明“tom”对象。我建议阅读有关方法的教程,例如,对象“tom”的可能副本是Main()方法的本地副本。此对象只能在main()方法中访问。因此,您无法访问作用域之外的本地成员,即tom_details()方法内部。此问题有两种解决方案。1) 将“tom”对象传递给tom_details()方法。2) 使用“static”关键字在主类中全局声明“tom”对象。