types - How to make a typeless/generalized parameter in Java? -
Say that I have 2 ways: add one to two int and the other two. How the parameters are passed in the function.
How can I make a method that generalizes the parameter and return value to it?
I saw one such move in GWT:
public Fixed & lt; T & gt; T plus (long A, long b) {return (t) (object) (a + b); }
Use it like this:
tall answer = addition (1l, 2l); Int answer2 = addition (1, 2);
Though it is asking for you, the downside of this approach is that it opens you up to a bad type of cast. Although you do not need to explicitly put the price to be returned, you are still effectively communicating the code that you can accidentally say something like this:
string t = addition (1, 2);
A runtime will be the cause of the exception. Even so, if you have a way whose return value is placed immediately after the return, then this move can clearly save the problem of casting the neck.
Comments
Post a Comment