//值傳遞
public class ValueDemo{
public static void main(String[] args){
int x=10;
method(x);
System.out.println("x="+x);
}
public static void method(int mx){
mx=20;
}
}
//示例二:引用傳遞
public class RefDemo1{
public static void main(String[] args){
Duck d = new Duck();
method(d);
System.out.println("Duck age ="+d.age);
}
public static void method(Duck duck){
duck.age = 5;
}
}
class Duck{
int age = 2;//省略封裝
}
/**示例三:String傳遞
字符串本身就是一個對象
*/
public class RefDemo2{
public static void main(String[] args){
String name ="小飛";
method(name);
System.out.println("name ="+name);
}
public static void method(String sname){
sname = "小備";
}
}
//示例三:String傳遞
public class RefDemo3{
public static void main(String[] args){
Person p = new Person();
method(p);
System.out.println("person name ="+p.name);
}
public static void method(Person per){
per.name = "備備";
}
}
class Person{
String name ="飛飛";//省略封裝
}
<meta charset="utf-8">
<meta charset="utf-8">