【问题标题】:Type Bounds: Apple and Orange类型界限:Apple 和 Orange
【发布时间】:2021-03-19 16:21:12
【问题描述】:
class Fruit implements Comparable<Fruit> {

    private final int weigth;

    public Fruit(int weight) {
        this.weigth = weight;
    }

    @Override
    public int compareTo(Fruit other) {
        return Integer.compare(this.weigth, other.weigth);
    }

    public int getWeigth() {
        return this.weigth;
    }
}

class Apple extends Fruit {

    public Apple(int weight) {
        super(weight);
    }

}

class Orange extends Fruit {

    public Orange(int weight) {
        super(weight);
    }
}

任务是重新设计类型系统并实现水果之间的正确比较(苹果与苹果,橙子与橙子,但不是苹果与橙子和橙子与苹果)。

并且“Apples with Oranges 和 Oranges with Apples 的比较应该在编译时进行限制”。

不知道怎么改,请指教。

【问题讨论】:

  • Fruit 无法再实现Comparable&lt;Fruit&gt;
  • 为什么不在基类中实现接口呢?
  • 我尝试在 Apple 和 Orange 类中实现它,但系统使用 Fruit apple = ..., Fruit orange = ..., 然后是 apple.compareTo(orange);
  • “但系统使用”并不能阻止“在编译时限制苹果与橙子和橙子与苹果的比较”的含义?
  • 哦,我真的很笨,对不起,我认为这意味着错误应该在 compareTo 函数的参数中。谢谢

标签: java generics comparator


【解决方案1】:

我建议向 Fruit 引入一个表示“SELF 类型..”的泛型

class Fruit<S extends Fruit> implements Comparable<S> {

            private final int weigth;

            public Fruit(int weight) {
                this.weigth = weight;
            }

            @Override
            public int compareTo(S other) {
                return Integer.compare(this.weigth, other.getWeigth());
            }

            public int getWeigth() {
                return this.weigth;
            }
        }

        class Apple extends Fruit<Apple> {

            public Apple(int weight) {
                super(weight);
            }

        }

        class Orange extends Fruit<Orange> {

            public Orange(int weight) {
                super(weight);
            }
        }
    }

证明:

【讨论】:

  • 它仍然比较苹果和橘子
  • 可能是在尝试我的样品时发生了错误?我只是添加了一个截图来证明它没有编译
【解决方案2】:
class Fruit {

private final int weigth;

public Fruit(int weight) {
    this.weigth = weight;
}


public int getWeigth() {
    return this.weigth;
}

}

class Apple extends Fruit implements Comparable<Apple> {

public Apple(int weight) {
    super(weight);
}

@Override
public int compareTo(Apple o){
    return Integer.compare(this.getWeigth(), o.getWeigth());
}

}

class Orange extends Fruit implements Comparable<Orange>{

public Orange(int weight) {
    super(weight);
}

@Override
public int compareTo(Orange o){
    return Integer.compare(this.getWeigth(), o.getWeigth());
}

}

【讨论】:

    【解决方案3】:

    如果两个水果不能比较,那为什么要实现 Comparable? Comparable 应该在具体的类级别定义,这样它将解决您的问题。

    或者如果你想检查现有的课程,你可以做类似的事情

    @Override
    public int compareTo(Fruit other) {
        if(other.getClass().equals(this.getClass())){
            return Integer.compare(this.weigth, other.weigth);
        }
           throw new NotComparableException("Object are of different class, and so can't be compared");
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多