赞
踩
我一直认为具有相同方法的两个接口不能被一个类继承,正如很多问题所述.
Java jre 7,jdk 1.7
但这里的代码是有效的.
例:
接口:
public interface IObject
{
public Object create(Object object) throws ExceptionInherit;
}
public interface IGeneric
{
public T create(T t) throws ExceptionSuper;
}
实施班级:
public class Test implements IGeneric, IObject
{
@Override
public Object create(final Object object) throws ExceptionInherit
{
return object;
}
}
那两个方法声明不一样吗?
例外:
例外只是这个结构的附加物,使其更加复杂.
public class ExceptionSuper extends Exception {}
public class ExceptionInherit extends ExceptionSuper {}
它的工作原理没有任何抛出异常.
进一步:如果两个方法接口抛出不同的继承异常,我可以将UserLogic强制转换为两个接口中的任何一个,并检索异常的不同子集!
这为什么有效?
编辑:
The generic implementation is not even necessary:
public interface IA
{
public void print(String arg);
}
public interface IB
{
public void print(String arg);
}
public class Test implements IA, IB
{
@Override
public void print(String arg);
{
// print arg
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。