java 板


LINE

※ 引述《egheee (阿平)》之铭言: : 标题: [问题] 多继承super的问题 : 时间: Fri Dec 19 12:15:54 2014 : : class A { : : void tell() { : Log.e("", "I am a"); : } : } : : class B extends A{ : : void tell() { : Log.e("", "I am b"); : } : } : : class C extends B { : : void tell() { : super.super.tell(); // 问题 : Log.e("", "I am c"); : } : } : : 如上列所示,这样的写法是有问题的,super好像规定只能用一次 : : 请问我要怎麽从C里面call到A的tell()呢? 推文有提到如果C要call A的tell(),何不直接继承A,或另写方法,或建立物件。 这些都要改写原程式码。 bleed1979看了一下程式码,觉得大概也只能改写了。 提供我的建议(依照改动程度小至大): 1.建立物件: class C extends B { void tell() { (new A()).tell(); out.println("this is C"); } } 2.另写方法: class B extends A { void tell() { out.println("this is B"); } void tellByA() { super.tell(); } } class C extends B { void tell() { tellByA(); out.println("this is C"); } } 3.static: class A { static void tell() { out.println("this is A"); } } class B extends A { static void tell() { out.println("this is B"); } } class C extends B { static void tell() { A.tell(); out.println("this is C"); } } 特别注意,bleed1979反对改写为直接继承A。 这个提议虽然是可以直接呼叫A的tell(), 但动到原有架构,为不可行的方案。 应该还有其他方法,就待版友补完吧。 : : -- :



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 124.219.31.93
: ※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1418962556.A.4EB.html : → sCHb68: 在 class C 正确用一个 super , 12/19 12:31 : → sCHb68: 然後在 class B 再使用 super 不知能否解决原PO问题。 12/19 12:31 : → egheee: 我就是想要跳过B直接call A的,这样的方式会连B一起call到 12/19 14:06 : 推 gpctv: 那为何不直接在C建立物件就可直接call C的tell方法啦?! 12/19 14:55 : 推 gpctv: 不晓得这样你懂不懂我的意思 12/19 14:58 : → ssccg: class封装来说,继承就是取得parent的非private的member 12/19 15:04 : → ssccg: 但是parent又去继承了什麽东西是parent class的实作细节 12/19 15:04 : → ssccg: child class知道parent有tell(),但不该知道parent的第几层 12/19 15:05 : → ssccg: parent有个tell(),那不是由继承能使用的介面 12/19 15:05 : → ssccg: 你想跳过B这件事就有问题了,C本来就不该知道B是继承了什麽 12/19 15:07 : → ssccg: C只知道B提供什麽,不知道那是B自己实做还是继承来的 12/19 15:08 : 推 Peruheru: 所以你如果要A的什麽,你应该是去继承A,而不是继承B 12/19 15:08 : → Peruheru: 不然就是B当初设计就要为A的tell()另外提供一个方法 12/19 15:10 : → Peruheru: 比如说tellbyA之类的 12/19 15:11 : → Peruheru: B表示:朕不给的,你不能要 12/19 15:11 : → swpoker: has is use 12/19 15:14 : 推 gpctv: 楼上说的其实很有道理,但是你硬要这样做那就建立物件吧! 12/19 15:36 : → gpctv: http://pastie.org/9789546 12/19 15:36 -- 看到这类问题,bleed1979就真的bleed了。 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.203.156
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1418986404.A.6BD.html
1F:→ adrianshum: (new A()).tell() 错得离谱。意思完全不一样好不好 12/30 22:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP