http://stackoverflow.com/questions/8422842/in-java-syntax-class-extends-something
There are a few confusing answers here so I will try and clear this up. You define a generic as such:
public class Foo<T> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
If you want a generic on Foo to always extend a class Bar you would declare it as such:
public class Foo<T extends Bar> {
private T t;
public void setValue(T t) {
this.t = t;
}
public T getValue() {
return t;
}
}
The ?
is used when you declare a variable.
Foo<? extends Bar>foo = getFoo();
OR
DoSomething(List<? extends Bar> listOfBarObjects) {
//internals
}
'Java' 카테고리의 다른 글
[자바]Velocity Sample (0) | 2014.08.19 |
---|---|
[Java] Today, Yesterday, This week, Last week, This Month,... (0) | 2014.08.19 |
[자바]Class<? extends Something> 사용법에 대하여 (0) | 2014.07.09 |