在开发满足自己需要的类的体系结构时,我遇到了这种情况。我有一个抽象类,其中包含一些必须由子类实现的方法,但是在子类中,我发现我需要使用从头继承的签名来实现。
为了向您展示我的意思:
// person class
public abstract class Person
{
protected void abstract workWith(Object o) throws Exception;
}
//developer class
public class Developer extends Person
{// i want to implement this method with Computer parametre instead of Object and throws `//DeveloperException instead of Exception`
protected void workWith(Computer o) throws DeveloperException
{
//some code here lol install linux ide server and stuff
}
}
// exception class
public class DeveloperException extends Exception
{
}
有什么办法吗?我不知道泛型是否有可能。太感谢了。
您绝对可以为此使用泛型:
public abstract class Person<T, U extends Exception> {
protected abstract void workWith(T t) throws U;
}
class Developer extends Person<Computer, DeveloperException> {
protected void workWith(Computer c) throws DeveloperException {
//implementation code
}
}
这样就可以完成您想要的,但是我们需要有关您的用例的更多详细信息,以确定这是否是正确的设计。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句