简体   繁体   English

在实现接口的abstact类中使用泛型

[英]Using generics in abstact class that implements interface

I haven't worked with generics yet so I'm not sure how to use them in my situation. 我还没有使用泛型,所以我不确定如何在我的情况下使用它们。 What I'm trying to achieve is to create a superclass Vector that would use generics in it and then 3 subclasses where generics type will be set as one of dimensions. 我要实现的目标是创建一个将在其中使用泛型的超类Vector,然后创建3个子类,其中将泛型类型设置为维之一。 Here is what I have right now: 这是我现在所拥有的:

interface sample {
    Vector sum(Vector vec);

    Vector subtraction(Vector vec);

    int product(Vector vec);

    boolean compare(Vector vec);

    String ToString();
}

abstract class Vector implements sample {
    int[] coordinates;

    public Vector(int[] coordinates) {
        this.coordinates = coordinates;
    }

    abstract Vector resVec();

    public Vector sum(Vector vec) {
        Vector result = resVec();
        if (this.coordinates.length == vec.coordinates.length) {
            for (int i = 0; i < vec.coordinates.length; i++) {
                result.coordinates[i] = this.coordinates[i] + vec.coordinates[i];
            }
        } else {
            throw new ArithmeticException("Can't sum vectors of different length");
        }
        return result;
    }

and two subclasses for example: 1) 和两个子类,例如:1)

class Vector3D extends Vector {

    public Vector3D(int n1, int n2, int n3) {
        super(new int[]{n1, n2, n3});
    }

    public Vector3D resVec() {
        Vector3D resVec = new Vector3D(0, 0, 0);
        return resVec;
    }

    public Vector3D sum(Vector vec) {
        return (Vector3D) super.sum(vec);
    }

2) 2)

class Vector5D extends Vector {
    public Vector5D(int n1, int n2, int n3, int n4, int n5) {
        super(new int[]{n1, n2, n3, n4, n5});
    }

    public Vector5D resVec() {
        Vector5D resVec = new Vector5D(0, 0, 0, 0, 0);
        return resVec;
    }

    public Vector5D sum(Vector vec) {
        return (Vector5D) super.sum(vec);
    }

So what I want to get as a result is for example if I type in main something like this: Vector3D vec1 = new Vector3D(1,2,3); 因此,我想得到的结果是,例如,如果我输入如下主要内容:Vector3D vec1 = new Vector3D(1,2,3); Vector5D vec2 = new Vector5D(1,2,3,4,5); Vector5D vec2 =新的Vector5D(1,2,3,4,5); and use A.sum(B); 并使用A.sum(B); I should get a compile error because of wrong type of variable has been passed to sum method. 由于将错误的变量类型传递给sum方法,我应该得到一个编译错误。 Thanks! 谢谢!

That's not the responsibility of the type hierarchy; 这不是类型层次结构的责任。 that's the responsibility of the concrete class. 这是具体班级的责任。

The big thing here is that your sum method is accepting a type of Vector , and the implementors of that sum method should know what is and is not a valid type that they can perform against. 这里最大的事情是您的sum方法正在接受Vector的类型,并且该sum方法的实现者应该知道什么是有效类型,哪些不是可以对其执行的有效类型。 Adding an instanceof check to your sum method should be all you realistically need. 实际需要的只是向您的sum方法添加instanceof check。

Generics would help if you wanted to ensure type homogeneity across a collection of vectors, but I don't see it being of much value in this case when you know for a fact what each vector is capable of operating on. 如果您想确保向量集合之间的类型同质性,则泛型会有所帮助,但是当您知道每个向量可以执行的操作时,在这种情况下,我认为它没有太大的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM