[問題] 關於物件的"="是什麼概念?
public class QQ {
public static void main(String[] args){
child c1 =new child();
parent p1 =new parent();
System.out.println(p1.toString()+" "+c1.toString());
System.out.println("p1.i: "+p1.i);
p1.show();
System.out.println("c1.i: "+c1.i);
c1.show();
System.out.println("");
p1=c1;
System.out.println(p1.toString()+" "+c1.toString());
System.out.println("p1.i: "+p1.i);
p1.show();
System.out.println("c1.i: "+c1.i);
c1.show();
System.out.println("");
}
}
class parent{
int i;
parent(){}
void show(){
System.out.println("show p1.i: "+i);
}
}
class child extends parent{
int i=1;
child(){}
void show(){
System.out.println("show c1.i: "+i);
}
}
result:
parent@14a55f2 child@15093f1
p1.i: 0
show p1.i: 0
c1.i: 1
show c1.i: 1
child@15093f1 child@15093f1
p1.i: 0 <<~~!?
show c1.i: 1
c1.i: 1
show c1.i: 1
在執行p1=c1後
透過hashCode可以看到p1和c1已經變成同樣內容
為何要顯示p.i時還是指向parent的class?
還是說p1=c1只有methodc會被蓋掉,本身成員變數不會?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.136.150.29
推
03/26 12:39, , 1F
03/26 12:39, 1F
→
03/26 12:56, , 2F
03/26 12:56, 2F
推
03/26 13:18, , 3F
03/26 13:18, 3F
→
03/26 13:23, , 4F
03/26 13:23, 4F
→
03/26 13:28, , 5F
03/26 13:28, 5F
→
03/26 13:56, , 6F
03/26 13:56, 6F
→
03/26 16:51, , 7F
03/26 16:51, 7F
推
03/26 17:23, , 8F
03/26 17:23, 8F
→
03/26 17:54, , 9F
03/26 17:54, 9F
推
03/26 19:54, , 10F
03/26 19:54, 10F
推
03/26 23:13, , 11F
03/26 23:13, 11F
推
03/27 16:32, , 12F
03/27 16:32, 12F
→
03/27 16:35, , 13F
03/27 16:35, 13F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):