package ch14;
class Message{
private int num;
void setNum(int num){
this.num = num;
}
int getNum(){
return this.num;
}
}
public class ch14_1 {
public static void main(String[] args) {
Message msg = new Message();
msg.setNum(100);
fun(msg);
System.out.println(msg.getNum());
}
public static void fun(Message temp){//引用传递
temp.setNum(30);
}
}
/*public class ch14_1{
public static void main(String[] args) {
String str = "hello";
fun(str);
System.out.println(str);
}
private static void fun(String temp){
temp = "world";
}
}*/
为什么第一个程序可以将值改为30,第二个程序无法将字符串改为world?
第一个程序里用了private封装把,封装有什么用,求详细解答