Re: [問題] 關於物件的"="是什麼概念?
這題是繼承和有沒使用到多型的問題。
有請打開Debugger,以Eclipse為例
在child c1 =new child();的下一行下斷點。
然後打開c1,你會發現有兩個i,一個是來自parent,一個是child本身。
什麼?你不相信我?
為證明我說的,我把程式稍微加料一下。
在parent p1 =new parent();的下一行加上c1.showSuper();
然後在class child裡面加上
void showSuper() {
System.out.println("show c1.super.i: "+super.i);
}
這樣就應該了解了。
另外,
如果你有使用C/C++也可以把你原本的code改成C/C++的版本。
像是Visual Studio 2012 Desktop Expression是免費的,
打開Debugger來跑,你會看到child裡面還會有一個parent的分支可以打開。
這個Debugger算是做的比較清楚的。
最後,
雖然Java沒辦法操作指標,但物件指向相當於指標指向的概念。
那toString()是把指標的值印出來。
而C/C++有個%p可以把指標的值印出來
所以你在C/C++下
1 cout << "address of p = " << &p << endl;
2 cout << "address of c = " << &c << endl;
3 printf("value of p = %p\n", p);
4 printf("value of c = %p\n", c);
列1, 2永遠不會變的,就是指標本身在記憶體裡面的位址。
列3, 4在p轉向c之後,兩個值會一樣。就是toString()所呈現的。
※ 引述《orze04 (orz)》之銘言:
: 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: 114.43.112.210
推
03/26 19:17, , 1F
03/26 19:17, 1F
→
03/26 19:20, , 2F
03/26 19:20, 2F
推
03/26 19:39, , 3F
03/26 19:39, 3F
→
03/26 19:55, , 4F
03/26 19:55, 4F
推
03/26 20:09, , 5F
03/26 20:09, 5F
→
03/26 20:17, , 6F
03/26 20:17, 6F
→
03/27 02:03, , 7F
03/27 02:03, 7F
→
03/27 02:06, , 8F
03/27 02:06, 8F
→
03/27 02:07, , 9F
03/27 02:07, 9F
→
03/27 02:12, , 10F
03/27 02:12, 10F
→
03/27 02:14, , 11F
03/27 02:14, 11F
→
03/27 02:24, , 12F
03/27 02:24, 12F
→
03/27 02:33, , 13F
03/27 02:33, 13F
→
03/27 02:44, , 14F
03/27 02:44, 14F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):