简体   繁体   English

Java:使用泛型指定类型的单个子类型

[英]Java: Specifying a single subtype of a type with Generics

I have an interface called DrawableSegment and multiple classes which implement that interface, for example LineSegment and PercentageSegment . 我有一个称为DrawableSegment接口,以及多个实现该接口的类,例如LineSegmentPercentageSegment

I also have a another class called BarChart which makes use of these classes. 我还有另一个名为BarChart类,它利用了这些类。 For instance BarChart has a method called Add(DrawableSegment segment) which accepts any object which implements that interface. 例如, BarChart有一个称为Add(DrawableSegment segment)的方法,该方法可以接受实现该接口的任何对象。

I would like to restrict this to be objects of the same type which implements that interface. 我想将其限制为实现该接口的相同类型的对象。 So I do not want to mix LineSegments with PercentageSegments , if I add in a LineSegment I want the rest of the additions to also be LineSegments . 因此,我不想将LineSegmentsPercentageSegments混合使用,如果我添加LineSegment我希望其余的添加项也都是LineSegments If I add in PercentageSegments , I would like the rest of them to be PercentageSegments , if I did attempt to add in a LineSegment I would like it to be an type error. 如果我添加PercentageSegments ,我希望其余的都是PercentageSegments ,如果我确实尝试添加LineSegment我希望它是类型错误。

Is there a way I can express this? 有什么办法可以表达我吗?

Is this what you mean? 你是这个意思吗?

import java.util.ArrayList;
import java.util.List;

interface DrawableSegment {}

class LineSegment implements  DrawableSegment {}

class PercentageSegment implements  DrawableSegment {}

class BarChart<T extends DrawableSegment> {
    private List<T> drawableSegments = new ArrayList<>();

    public void add(T drawableSegment) {
        this.drawableSegments.add(drawableSegment);
    }

    public List<T> getDrawableSegments() {
        return this.drawableSegments;
    }
}

public static void main(String[] args) {
    BarChart<LineSegment> barCharLineSegment = new BarChart<LineSegment>();
    barCharLineSegment.add(new LineSegment());
    barCharLineSegment.add(new PercentageSegment()); // Compiler Error: cannot be applied
}

If I understood your question correctly, you want to check the type of the object at runtime , meaning if a LineSegment is added then the subsequent aditions would only allow LineSegment and not PercentageSegment . 如果我正确理解了您的问题,那么您想在运行时检查对象的类型,这意味着如果添加了LineSegment ,则后续分配仅允许LineSegment而不允许PercentageSegment In that case, at runtime we can check what is the class of the first item and compare it with the next items. 在这种情况下,我们可以在运行时检查第一个项目的类,然后将其与下一个项目进行比较。 Please see if my code helps: 请查看我的代码是否有帮助:

interface DrawableSegment {}

class LineSegment implements  DrawableSegment {}

class PercentageSegment implements  DrawableSegment {}

class Barchart<T extends DrawableSegment> {
    List<DrawableSegment> drawableSegList = new ArrayList<>();

    public static void main(String[] args) throws IOException  {
       Barchart main = new Barchart();
       main.add(new LineSegment());
       System.out.println(main.getListSize());
       main.add(new LineSegment());
       System.out.println(main.getListSize());
       main.add(new PercentageSegment());
       System.out.println(main.getListSize());
    }
    public void add(T t){
        if(!drawableSegList.isEmpty() && drawableSegList.get(0).getClass() != t.getClass()){
            System.out.println("First element :"+ drawableSegList.get(0).getClass()+" Does not match next Element "+t.getClass());
            throw new RuntimeException();
        }
        drawableSegList.add(t);
    }
    public int getListSize(){
        return this.drawableSegList.size();
    }
}

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

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