方法がありますfoo
void foo (String x) { ... }
void foo (Integer x) { ... }
引数を気にしないメソッドから呼び出したい:
void bar (Iterable i) {
...
for (Object x : i) foo(x); // this is the only time i is used
...
}
上記のコードは、それfoo(Object)
が定義されておらず、追加すると不平を言います
void foo (Object x) { throw new Exception; }
次にbar(Iterable<String>)
、代わりにそれを呼び出してfoo(String)
例外をスローします。
bar(Iterable<String>)
と の 2 つのテキストが同一の定義を避けるにはどうすればよいbar(Iterable<Integer>)
ですか?
私は次のようなもので逃げることができると思った
<T> void bar (Iterable<T> i) {
...
for (T x : i) foo(x); // this is the only time i is used
...
}
しかし、その後cannot find foo(T)
エラーが発生します。