2 回答

TA貢獻1735條經(jīng)驗 獲得超5個贊
雖然您可以單獨傳遞變量,但傳遞對整個Student對象的引用可能更有意義。例如:
public static void main(String[] args) {
Student tom = new Student();
tom.name = "Tom";
tom.id = 1;
tom.age = 15;
tom.year = 10;
printDetails(tom);
}
private static void printDetails(Student student) {
System.out.println(student.name);
System.out.println(student.id);
System.out.println(student.age);
System.out.println(student.year);
}
之后我要采取的下一步措施是:
給出
Student
一個接受姓名、ID、年齡和年份的構(gòu)造函數(shù)將所有字段設(shè)為
Student
私有(并且可能是最終的),而不是通過方法公開數(shù)據(jù)(例如getName()
)可能會在其中添加一個
printDetails()
方法Student
,以便您可以直接調(diào)用tom.printDetails()
您的main
方法。

TA貢獻1836條經(jīng)驗 獲得超4個贊
我認為你可以只傳遞對象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);
}
添加回答
舉報