I'm having trouble figuring out how to properly define Generics. Could you let me know what I'm doing wrong here?
public class MyClass1<T> { private final MyClass2<T> myobject; private final T startValue; public MyClass1(T value, MyClass2<T> obj) { startValue = value; myobject = obj; } public void run() { for ( T i = startValue; i < startValue+3; i++ ) myobject.run(i); } }
Then I call it with this:
MyClass1<int> myclass1i = new MyClass1<int> ( 1 , myObj); MyClass1<char> myclass1c = new MyClass1<char>('a', myObj);
I'm sure there are some syntax mistakes, but there is also the problem that i++ isn't recognized. This would be easy in C++ as I'd just have to ensure that any classes which act as the template type need to have ++ overloaded (int and char do support these). I'd only get a compiler error if I used an object which doesn't support this. I'm not sure that it's this easy in Java.