I have two formats of a class BaseButton
:
public abstract class BaseButton<E extends BaseButton<E>> extends JXButton implements Initializer {
private static final long serialVersionUID = 11L;
public BaseButton() {
LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
initialize();
}
public abstract void initialize();
}
And:
public abstract class BaseButton<E> extends JXButton implements Initializer {
private static final long serialVersionUID = 11L;
public BaseButton() {
LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
initialize();
}
public abstract void initialize();
}
And I am creating child classes as:
public class GreenButton extends BaseButton<GreenButton> {
}
My question is what is the difference between these two types of generic notation?
Which one is better and why?
Any information will be very helpful to me.
Thanks.
With the code you provided, it makes no difference.
However, when you start using the generic type E
, that's when you'll start seeing a difference. In the first example, E
will have all the methods and members of BaseButton
. So, if BaseButton
has a method int foo(String s)
, you will be able to say:
int bar(E arg) {
arg.foo("test");
}
If you try the same thing with the second example, the compiler will report an error, because E
implicitly extends Object
and Object
does not have a foo
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.