爲了澄清@tzhechev,對象不是通過Java中的引用傳遞的,而是通過Java中的值傳遞對象引用,這在Abu的答案中非常明確。爲了更清楚地說明這個話題,請看這個例子。
class Employee
{
String name;
int age;
}
public class ValueOrReference
{
public static void main(String... args)
{
Employee emp1 = new Employee();
emp1.name = "Myself";
emp1.age = 1;
Employee emp2 = new Employee();
emp2.name = "Yourself";
emp2.age = 2;
System.out.println("Before swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
/*
* Now if Objects are passed by Reference
* then if I swap the two objects in a method,
* then the actual objects will reciprocate
* to the said change. Lets find out.
*/
swapObjects(emp1, emp2);
System.out.println("****************************************");
System.out.println("Inside main after swap method.");
System.out.println("After swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
System.out.println("****************************************");
}
private static void swapObjects(Employee emp1, Employee emp2)
{
System.out.println("****************************************");
System.out.println("Inside the swap method.");
Employee temp = emp1;
emp1 = emp2;
emp2 = temp;
System.out.println("After swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("Employee 2 : ");
System.out.println("Name : " + emp2.name);
System.out.println("Age : " + emp2.age);
System.out.println("****************************************");
}
}
輸出案例1:
C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************
Inside the swap method.
After swapping values are :
Employee 1 :
Name : Yourself
Age : 2
Employee 2 :
Name : Myself
Age : 1
****************************************
****************************************
Inside main after swap method.
After swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************
但如果我只使用一個對象,然後修改它的內容在一些方法,那麼結果將是驚人的。嘗試爲驚喜,下面的代碼:
class Employee
{
String name;
int age;
}
public class ValueOrReference
{
public static void main(String... args)
{
Employee emp1 = new Employee();
emp1.name = "Myself";
emp1.age = 1;
System.out.println("Before swapping values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
/*
* Now if Objects are passed by Reference
* then if I swap the two objects in a method,
* then the actual objects will reciprocate
* to the said change. Lets find out.
*/
modifyObject(emp1);
System.out.println("****************************************");
System.out.println("Inside main after modify method.");
System.out.println("After modifying values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("****************************************");
}
private static void modifyObject(Employee emp1)
{
System.out.println("****************************************");
System.out.println("Inside the modify method.");
emp1.name = "Yourself";
emp1.age = 2;
System.out.println("After modifying values are : ");
System.out.println("Employee 1 : ");
System.out.println("Name : " + emp1.name);
System.out.println("Age : " + emp1.age);
System.out.println("****************************************");
}
}
OUTPUT案例2:
C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
****************************************
Inside the modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************
****************************************
Inside main after modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************
沒有人明白爲什麼你不認爲它會工作。這不是很好的代碼,但每個語句都很清楚它的功能。 addComponentsToFrame將組件添加到您在參數中提供的內容框架。一旦它們被添加,框架就被打包並顯示(setVisible)。非常簡單。 – Pete855217
但我明白爲什麼Stegeman不認爲它會起作用;這個問題也很清楚他認爲會發生什麼,並且很容易解釋哪些假設是錯誤的。他所得到的答案解決了他明確的問題,而你錯誤地認爲「沒人理解」。 – arcy
我試圖簡化代碼以保持簡短的問題並顯示相關的語句。 Tzhechev先生明白我的意思。 (忍者) – Stegeman