Re: [問題] 請問interface到底是做什麼用的?
※ 引述《sandwichC ( )》之銘言:
: 一種可能的解法如下 (如有更好的解法 還請賜教)
: interface IGoodAtSports{...}
: interface IGoodAtSinging{...}
: class father implements IGoodAtSports{...}
: class mother implements IGoodAtSinging{...}
: class child implements IGoodAtSports, IGoodAtSinging{...}
: 但若想讓father中sports的行為與child中sports的行為一模一樣 及
: 讓mother中singing的行為與child中singing的行為一模一樣
: 除了複製一次 還有什麼解法嗎?
: 我很久以前在java還programming版問過
: 但最後似乎就無疾而終了…
: 若有人想到好解法還請不吝賜教
這樣如何?
interface IGoodAtSinging{
public void sing();
}
class ImplementOfSinging implements IGoodAtSinging{
public void sing(){ out.prinln("I am singing"); }
}
class Father implements IGoodAtSinging{
private ImplementOfSinging imp_sing_ = new ImplementOfSinging();
public void sing(){ imp_sing_.sing(); }
}
class Child implements IGoodAtSinging{
private ImplementOfSinging imp_sing_ = new ImplementOfSinging();
public void sing(){ imp_sing_.sing(); }
}
--
Nobody can take anything away from him.
Nor can anyone give anything to him.
What came from the sea,
has returned to the sea.
Chrono Cross
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.210.113
討論串 (同標題文章)
完整討論串 (本文為第 17 之 33 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章