我有一个类似于 - 的接口层次结构
interface Type
{
Type copy();
};
interface SubType extends Type
{
};
interface SubSubType extends SubType
{
};
Run Code Online (Sandbox Code Playgroud)
还有一个具体的课程
public class Concrete implements SubSubType
{
@Override public Concrete copy() { return new Concrete(); }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在Type上调用copy()并获取Type返回,在SubType上调用copy()并获取SubType,等等.这是否可以实现?可能有仿制药?
提前致谢.
像这样:
public interface Type {
public Type copy();
}
public interface SubType extends Type {
public SubType copy();
}
public class Concrete implements Type {
@Override
public Concrete copy() {
return new Concrete();
}
}
public class SubConcrete implements SubType {
@Override
public SubConcrete copy() {
return new SubConcrete();
}
}
public static void main(String[] args) {
Type type = new Concrete();
SubType subType = new SubConcrete();
Type newType = type.copy();
SubType newSubType = subType.copy();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |