0

我项目的第一部分是构建一个超图

这是一个快速绘制的 UML 图在此处输入图像描述

顶点类

public abstract class Vertex <T>{

    int vertexId ;
    T vertexValue ;
    public  abstract  T computeVertexValue();
}

图像顶点类

public class ImageVertex extends Vertex<Map<String, Instance>>{


    public ImageVertex(int id ) {
        this.vertexId=id;
    }

    @Override
    public Map<String, Instance> computeVertexValue(){
        return null;
    }
}

抽象顶点工厂

public abstract class AbstractVertexFactory {

    public abstract  Vertex createVertex(int id);

    public  Vertex produceVertex(int id) {

        Vertex vertex = createVertex(id);
        vertex.computeVertexValue();
        return vertex;
    }
}

ImageFactory 类

public class ImageFactory extends AbstractVertexFactory {

    @Override
    public Vertex createVertex(int id) {
        // TODO Auto-generated method stub
        return new ImageVertex(id);
    }
}

模拟器

public class ImageFactorySimulator {

    /**
     * @param args
     */
    public static void main(String[] args) {


        AbstractVertexFactory imFactory= new ImageFactory();

        ImageVertex im = (ImageVertex) imFactory.createVertex(0);

    }

}

在模拟器中使用 cast 是有问题的 我该如何避免呢?

4

4 回答 4

2

你可以使用

public abstract class AbstractVertexFactory <T extends Vertex> {
    public abstract  T createVertex(int id);
}

public class ImageFactory extends AbstractVertexFactory<ImageVertex> {

    @Override
    public ImageVertex createVertex(int id) {
        // TODO Auto-generated method stub
        return new ImageVertex(id);
    }
}
于 2013-04-12T10:35:25.273 回答
1

尝试

abstract class AbstractVertexFactory<T extends Vertex> {
    public abstract Vertex createVertex(int id);
}

class ImageFactory extends AbstractVertexFactory<ImageVertex> {
    @Override
    public ImageVertex createVertex(int id) {
        return new ImageVertex(id);
    }
}
于 2013-04-12T10:35:50.413 回答
1

另一个答案是:

public abstract class Vertex <T>{

    int vertexId ;
    T vertexValue ;
    public  abstract  T computeVertexValue();
}
public class ImageVertex extends Vertex<Map<String, Object>>{


    public ImageVertex(int id ) {
        this.vertexId=id;
    }

    @Override
    public Map<String, Object> computeVertexValue(){
        return null;
    }
}
public abstract class AbstractVertexFactory<T extends Vertex> {

    public abstract  T createVertex(int id);

    public  T produceVertex(int id) {

        T vertex = createVertex(id);
        vertex.computeVertexValue();
        return vertex;
    }
}

public class ImageFactory extends AbstractVertexFactory<ImageVertex> {

    @Override
    public ImageVertex createVertex(int id) {
        // TODO Auto-generated method stub
        return new ImageVertex(id);
    }
}

public class ImageFactorySimulator {

    /**
     * @param args
     */
    public void ttt(String[] args) {


        ImageFactory imFactory= new ImageFactory();

        ImageVertex im = imFactory.createVertex(0);

    }

}
于 2013-04-12T10:46:24.790 回答
0

你可以使用这个:

public class ImageFactory extends AbstractVertexFactory {

    @Override
    public Vertex createVertex(int id) {
        // TODO Auto-generated method stub
        return createImageVertex(id);
    }

    public ImageVertex createImageVertex(int id) {
        // TODO Auto-generated method stub
        return new ImageVertex(id);
    }
}

public class ImageFactorySimulator {

    /**
         * @param args
     */
    public static void main(String[] args) {


        AbstractVertexFactory imFactory= new ImageFactory();

        ImageVertex im = imFactory.createImageVertex(0);

    }
}
于 2013-04-12T10:36:14.990 回答