For example, in C#, Foo, Foo<T>, Foo<T1, T2> could have similar semantic but are different classes. But in Java, I can’t name classes like that. How to name them? I don’t think Foo, Foo1<T>, Foo2<T1, T2> are appropriate. Any other suggestions?
Actually, I want to design some interfaces which have only one method just like Runnable. But run in Runnable doesn’t have return value and any parameters. I want interfaces like
public interface Runnable<T>{
void run(T t);
}
public interface Runnable<T, TResult>{
TResult run(T t);
}
public interface Runnable<T1, T2, TResult>{
TResult run(T1 t1, T2 t2);
}
And so on. And I think they are better in the same package.
You can rename them to truly reflect their function:
you can even declare an interface, say Foo, that provides all the methods every well-behaved Foo should have. Then you implement the interface in different Foos with different type parameters:
EDIT: working at your examples, the 3 interfaces can really be declare just using one of them:
and if you don’t want any parameters, you just supply Void whenever necessary. A Void class in uninstantiable, so the only other possibility is to pass or return null. So for example:
creates a method similar to
You actually declared
but Void in uninstantiable, so you must pass nulls and the function will always return null.