Re: [問題] 物件導向
: 我想要用Abstract Class或是Interface去規範這些Method
: 但是因為相同名稱的Method在不同Class參數數量與型別都不一樣
: 所以不知道怎麼Define Abstract Class或是Interface
: 煩請有經驗的前輩給予小弟一些指導
: 在這邊先謝謝各位前輩的幫忙
我猜你可能需要使用 generics,例如:
interface MyInterface<T1, T2>
{
public void doA(T1 o);
public void doB(T2 o);
}
class MyClassA implements MyInterface<AOne, ATwo>
{
public void doA(AOne o)
{ /* ... */ }
public void doB(ATwo o)
{ /* ... */ }
}
class AOne
{
String a;
int b
}
// ATwo 略過
class MyClassB implements MyInterface<BOne, BTwo>
{
public void doA(BOne o)
{ /* ... */ }
public void doB(BTwo o)
{ /* ... */ }
}
class BOne
{
int a;
int b;
}
// BTwo 略過
利用 generics 你就可以讓 MyClassA 和 MyClassB 有相同的 method 但他們接
受的 parameter 有不同的 type。
--
If I don't know I don't know, I think I know
If I don't know I know, I think I don't know
── R. D. Laing
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.217.65.84
討論串 (同標題文章)