Making Java methods generic
Suppose I have these abstract classes:
class BasicObject {开发者_Go百科
private String name;
// getter and setter here
}
class BasicObjectExplorer {
void explore(BasicObject basicObject) {
// now some actions regarding name property
}
}
Now you have AdvancedObject extending BasicObject and AdvancedObjectExplorer extending BasicObjectExplorer, How to use generics to reuse code of Basic classes and write just functionality regarding new properties added to AdvancedObject?
Thank you for help.
If you are asking that you want your abstract class to be base of class handling generic classes .
Then
public abstract class AbstractObjectExplorer<T> {
void explore(T basicObject) {
// now some actions regarding name property
}
}
and
public class Foo extends BasicObjectExplorer<String> {
}
public class Bar extends BasicObjectExplorer<Double> {
}
精彩评论