Re: 透過繼承來override static variab?
感謝大家耐心回覆我的問題
我必須要用static variable的原因是為了要節省空間
其實我原來比擬的"car"是一段encode binary string (DNA short fragment)
只占 8 byte. 在加個int 每個read就多占4 btye了
因為原來每個fragment 長度都相等. 只需一份 'statict variable'
現在需要多種不同長度的fragments set, 所以定義多個class 其他method相同
但是不同set 長度不同.. 所以要幾個class 有幾份自己的 "static variable"
我確實應該作個abstract class 然後每個在定義自己的static variables
比如說fragment30, fragment40, fragment50 .. fragment60..
30, 40, 50, 60是不同的 但這樣其實還滿困擾的 每個都要定一個用const
※ 引述《adrianshum (Alien)》之銘言:
: ※ 引述《sorryChen (陳揚和)》之銘言:
: : 假設我原來有個class ex: car.. 有個static variable 叫 num_of_wheel
: : 現在發現我要有很多種car 各有不同的 num_of_wheel.
: : (每種車wheel的個數是一樣的..)
: : 這樣透過 繼承override num_of_wheel 是一般的做法嗎
: 1) 既然你不同的 car 有不同的 num_of_wheel 就代表那不是 class attribute
: (static variable) 了.
: 2) 每輛車有不同的輪子數, 那num_of_wheel 很明顯就是 車子的 attribute 了,
: 該是 class Car 數面有 num_of_wheel 的 instance attribute
: 3) 常見的 OOPL, 沒法 "override" 一個 variable
: 4) 一般做法是把 num_of_wheel 作為 Car 的 member attribute, 要是真的
: 需要有不同類的車 (有時可能靠本身的 attribute 就夠表達, 那就不必
: 濫用繼承), 而 每種車都有該種車的輪子數量, 就在各類車子的 subclass
: 設自己的 num_of_wheel 好了
: 用別的例子, 比如動物有不同叫聲 (半 psuedo code)
: abstract class Animal { // 專作繼承用的
: private String m_voice;
: protected Animal(String voice) {
: m_voice = voice;
: }
: public void shout() {
: print("SHOUT: " + m_voice);
: }
: class Duck extends Animal {
: public Duck() {
: super("Quack");
: }
: }
: class Cat extends Animal {
: public Cat() {
: super("Meow");
: }
: }
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 207.151.231.15
→
07/30 15:01, , 1F
07/30 15:01, 1F
→
07/30 15:16, , 2F
07/30 15:16, 2F
推
07/30 21:05, , 3F
07/30 21:05, 3F
推
07/31 01:47, , 4F
07/31 01:47, 4F
→
08/01 13:21, , 5F
08/01 13:21, 5F
推
08/01 16:07, , 6F
08/01 16:07, 6F
推
08/01 16:07, , 7F
08/01 16:07, 7F
討論串 (同標題文章)