三个小问题

arthur.dy.lee arthur.dy.lee 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

package newGet;

public class D3 {
public static void changeDate_1(MyDate mydate) {
mydate = new MyDate(1979, 2, 3);
}

public static void changeDate_2(MyDate mydate) {
mydate.setMonth(12);

}

public static void main(String[] args) {
MyDate date = new MyDate(1981, 9, 10);
changeDate_1(date);
changeDate_2(date);
System.out.println(date.year + "-" + date.month + "-" + date.day);


}
}

class MyDate {
public int year;

public int month;

public int day;

public MyDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}

public void setMonth(int m) {
month = m;
}

public void setDay(int d) {
day = d;
}
}


输出是什么,重要的解释一下为何不是1979-12-3
如果执行:
String a=new String("1");
a=new String("2");
System.out.println(a);
输出肯定是2
两者有什么不同,都是在new对象呀?!

====================

class Cal{
private int x,y,z;
}
public class D6_exam {
public static void main(String[] args){
Cal tmp=new Cal();
tmp.x=10;tmp.y=20;
}
}

//========
public class D6_exam {
private int i=1;
private int j=2;
public static void main(String[] args){
System.out.println((new D6_exam()).i);
}
}


上面同是两代码访问私有属性的问题。为何下面的可以,上面的会报错。
...全文
评论 21
收藏 已收藏
分享
返回顶部
arthur.dy.lee
Java SE 62,620 成员 307,251 内容