java - Using generics to create max function that returns the larger one -
In Java, how do I use generic to create a maximum function, which are two similar types of comparative objects and larger returns One?
I tried:
public stable = 0) OBJ 1: OJ 2); }
(If both of them are equal then return to OBJE 1.)
This method is based on code that I have found.
However, when I compile it, I get this warning (with compilation - Xlint: uncheck): Warning: [uncheck] to compare unwanted calls (t) raw type As a member java.lang.Comparable
This is because you can use raw type Comparable, and the compiler can not be sure how you compare it to comparisons, comparative To use the area type parameter is also well require comparatively of Paramitryj:
public static & lt; T comparative & lt;? Super T & gt; & Gt; T max (T obj1, T obj2)
Do you need to use ? Since Super T
, you may have Class A which increases class B which is Comparable & lt; B & gt;
implies, but A Comparable & lt; A & gt; Does not apply to
. So can you pass the maximum of two objects, and the B matches ? Super A
, so that you can call the compare
method defined in B for A object. If you select & lt; T, then comparative & lt; T & gt; & Gt;
and two attempts to pass an object, then the compilation will complain that they are comparable & lt; A & gt;
, although they receive the compare
method from B
Comments
Post a Comment