作者TonyQ (骨头)
看板java
标题Re: [问题] Heterogeneous(异质)机制
时间Sat Mar 31 15:11:35 2007
: 0
: 是因为Base的default constructor都会把member attribute都设成0吗?
应该说 int 如果没给起始值,会自动给 0 .
: 还有如果我不加入那个showData()
: 错的原因是因为这种Heterogeneous机制
: Base a_base=new Derive();
: a_base只能呼叫Base里面的method吗?
因为它把a_base当成 Base类别的 instance,
所以他只能使用Base类别里的method,
它的可见范围就只有Base和 super class (Object) 里面的东西。
不过你也可以透过转型的方式使用啦,
适不适用只端看你的使用需求。
Base a_base;
a_base=new Derive();
((Derive)a_base).showData();
这样就不需要加上showData在Base内了
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.27.68