作者sorryChen (陈扬和)
看板Programming
标题Re: 透过继承来override static variab?
时间Thu Jul 30 12:24:13 2009
感谢大家耐心回覆我的问题
我必须要用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
1F:→ akasan:template阿孩子118.168.185.251 07/30 15:01
2F:→ akasan:当我说疯话 我的想法还是需要vtpr的空间XD118.168.185.251 07/30 15:16
3F:推 yoco315:template 没错..118.160.105.247 07/30 21:05
4F:推 march20:template+1 128.54.40.203 07/31 01:47
5F:→ sorryChen:对耶 我还以为template只有generic的功 128.125.87.33 08/01 13:21
6F:推 march20:楼上此话何解? 66.75.255.220 08/01 16:07
7F:推 march20:那你现在是拿 template 来做啥啊 @@ 66.75.255.220 08/01 16:07