As with many inheritance problems, I find it hard to explain what I want to do. But a quick (but peculiar) example should do the trick:
public interface Shell{
public double getSize();
}
public class TortoiseShell implements Shell{
...
public double getSize(){...} //implementing interface method
...
public Tortoise getTortoise(){...} //new method
...
}
public class ShellViewer<S extends Shell>{
S shell;
public ShellViewer(S shell){
this.shell = shell;
...
}
}
public class TortoiseShellViewer<T extends TortoiseShell> extends ShellViewer{
public TortoiseShellViewer(T tShell){
super(tShell); //no problems here...
}
private void removeTortoise(){
Tortoise t = tShell.getTortoise(); //ERROR: compiler can not find method in "Shell"
...
}
}
The compiler does not recognise that I want to use a specific implementation 开发者_如何学Pythonof Shell for getTortoise()
. Where have I gone wrong?
Based on what you've given here, the problem is that:
public class TortoiseShellViewer<T extends TortoiseShell> extends ShellViewer
does not specify ShellViewer
(which is generic) correctly. It should be:
public class TortoiseShellViewer<T extends TortoiseShell> extends ShellViewer<T>
You want this:
public class TortoiseShellViewer<T extends ToroiseShell> extends ShellViewer<T>
What is tShell in removeTortoise? Is it an instance of type Shell that is in the base class ShellViewer?
精彩评论