| | |
Generics and +=
Thread Solved |
Hello all, it's been a while since I've had a question, but I'm back.
I'm trying to write a Vector class which can be any type. The problem I have is that generics can only hold objects, so when I try to say x+=vec.x it says that it cannot do that, I tried to make sure that it extends number, but the autoboxing doesn't seem to work with generics.
can anyone help?
I'm trying to write a Vector class which can be any type. The problem I have is that generics can only hold objects, so when I try to say x+=vec.x it says that it cannot do that, I tried to make sure that it extends number, but the autoboxing doesn't seem to work with generics.
can anyone help?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
You might also explain what you mean by "cannot do that".
And explain exactly what "vec.x" is, as that is not how you access something in a vector.
But, of course, don't forget the actual code.
And explain exactly what "vec.x" is, as that is not how you access something in a vector.
But, of course, don't forget the actual code.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
what i mean by "cannot do that" is that because of the generic within the class trying to take the component x of type <T> and add it to another Java won't accept that as correct.
here is the code that is causing the problem
here is the code that is causing the problem
java Syntax (Toggle Plain Text)
package cannonfodder; /** * @author Bill * @param <T> type */ public class Vector <T extends Number> { T x, y; /** * @param x x * @param y y */ public Vector(T x,T y) { this.x=x; this.y=y; } /** * @param x x */ public void setX(T x) { this.x=x; } /** * @param y y */ public void setY(T y) { this.y=y; } /** * @param x x * @param y y */ public void set(T x, T y) { this.x=x; this.y=y; } /** * @param vec vector */ public void add(Vector<T> vec) { x+=vec.x; y+=vec.y; } /** * @param vec vector * @return dot */ public double dot(Vector vec) { return x*vec.x+y*vec.y; } /** * @param vec vector */ public void sub(Vector vec) { x-=vec.x; y-=vec.y; } /** * @param d d */ public void mult(double d) { x*=d; y*=d; } }
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
Join Date: Sep 2008
Posts: 1,568
Reputation:
Solved Threads: 196
The + operator only works with certain types. If you aren't using it with those types, then it isn't going to work. If you want more detailed reasoning on this, masijade is probably your man. But you should try using the floatValue method of the Number class, then adding the floats you get. But for example, Number could be a BigDecimal, BigInteger, etc (look at the doc) - which can't be added as simply as an int.
Nope, it can't, mainly because Number itself can't use those operators, only a little over half the classes that extend Number can use those operators (only through autoboxing of course, which Number, of course, also can't do).
The compiler can only work with what it knows it has, and in this case, that is "Number", and "Number", as already mentioned, cannot use those operators (nor can it be autoboxed, of course).
You can still "get away with" some of this by making this an abstract class and making the last three methods abstract, otherwise you will have to implement a somewhat involved case statement in those methods.
The compiler can only work with what it knows it has, and in this case, that is "Number", and "Number", as already mentioned, cannot use those operators (nor can it be autoboxed, of course).
You can still "get away with" some of this by making this an abstract class and making the last three methods abstract, otherwise you will have to implement a somewhat involved case statement in those methods.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Thanks, perhaps I'll just make it for only one type. The reason I wanted this is because I wanted to have a position in an array as a vector which has to be an int, but I also wanted to be able to have a vector that was in doubles for things like forces velocity and acceleration. but I suppose I will either have to make due with one, or write both separately. Thanks for the info.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
![]() |
Similar Threads
- Somethin to understand in Java Generics? (Java)
- Question regarding generics, help appreciated! (Java)
- generics in java (Java)
- Generics (Java)
- Generics? (Java)
Other Threads in the Java Forum
- Previous Thread: pack200 for reducing size of applets
- Next Thread: Java audio stream <<Very basic>>
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






