Java在实现接口中实现接口类型方法

时间:2015-09-26 19:38:08

标签: java interface reference-type

有一个名为Range的接口,如

public interface Range {
    public Range newRange(int from,int to);
    public boolean isIn(int value);
    public int min();
    public int max();
    public Range add(Range r);
}

在工具界面中,Range newRange(1,5)用于设置1到5的范围编号,方法Range add(Range r.newRange(6,8))根据方法{{添加范围从1到5加6到8。 1}}。如果值在此范围内,则返回Range newRange(1,5)boolean isIn(int value)返回范围中的最小值。如何使用class作为引用类型来实现方法?通过一个对象?我的int min()

newRange

我对这个问题一无所知,并且在课堂上作为参考类型有点混乱。感谢。

2 个答案:

答案 0 :(得分:0)

我认为您的界面不正确。 你称之为Range,但它里面可以有多个范围。

因此,为了获得良好的解决方案,请向Range添加方法以获取多个范围。

或者这里有一些解决方法:

import java.util.HashSet;
import java.util.Set;

public class RangeImpl implements Range {

    private class SimpleRange {
        public SimpleRange(int from, int to) {
            this.from = from;
            this.to = to;
        }
        final int from;
        final int to;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            SimpleRange that = (SimpleRange) o;

            if (from != that.from) return false;
            return to == that.to;

        }

        @Override
        public int hashCode() {
            int result = from;
            result = 31 * result + to;
            return result;
        }
    }

    private Set<SimpleRange> ranges=new HashSet<>();

    @Override
    public Range add(int from, int to) {
        ranges.add(new SimpleRange(from, to));
        return this;
    }


    @Override
    public int min() {
        return 0;
    }

    @Override
    public int max() {
        return 0;
    }

    @Override
    public Range add(Range r) {
        //1) first way If there is only 1 your implementation of Range.
        //extract ranges from inside
        if (ranges instanceof RangeImpl) {
            RangeImpl ri= (RangeImpl) r;
            ranges.addAll(ri.ranges);
        }
        return this;
    }



//    way2:
@Override
public boolean isIn(int value) {
    for (Range externalRange : externalRanges) {
        externalRange.isIn()
    }

    for (SimpleRange range : ranges) {
        range isin
    }
    return false;
}


    @Override
    Set<Range> externalRanges=new HashSet<>();
     //cache supplied ranges and use them in ither methods
    public Range add(Range r) {
        externalRanges.add(r);
        return this;
    }
}

答案 1 :(得分:0)

Here is your answer:

package com.genpact.java.interf;

public interface Range {

    /**
    * Create a new Range object representing an integer interval starting from 'from' and 'to', both limits inclusive
    */
    public Range newRange(int from,int to);

    /**
    * Return if 'value' is in the range defined by this object
    */
    public boolean isIn(int value);

    /**
    * Return the minimum value in range
    */
    public int min();

    /**
    * Return the maximum value in range
    */
    public int max();

    /**
    * Add range 'r' to this range, and return 'this'.
    * 'r' and this may denote disjoint ranges, for instance:
    *  r.newRange(1,5).add(r.newRange(8,10)) denotes a range
    * including 1,2,3,4,5,8,9,10

    */
    public Range add(Range r);


}

package com.genpact.java.impl;

import com.genpact.java.interf.Range;

public class RangeImplem implements Range {

    private int from;
    private int to;

    public RangeImplem() {
        // TODO Auto-generated constructor stub
    }

    public RangeImplem(int from, int to) {
        this.from = from;
        this.to = to;
    }

    @Override
    public Range newRange(int from, int to) {
        Range range=new RangeImplem(from, to);
        return range;
    }

    @Override
    public boolean isIn(int value) {
        //Return if 'value' is in the range defined by this object
        if(value >= this.from && value <= this.to){
            return true;
        }
        return false;
    }

    @Override
    public int min() {
        return this.from;
    }

    @Override
    public int max() {
        return this.to;
    }

    @Override
    public Range add(Range r) {
        this.from = r.min();
        this.to = r.min();
        return newRange(this.from,this.to);
    }


    public static void main(String[] args) {
        RangeImplem r=new RangeImplem();
        System.out.println(r.newRange(1,5).isIn(3)); //=> returns true
        System.out.println(r.newRange(1,5).isIn(6)); //=> returns false
        System.out.println(r.newRange(1,5).add(r.newRange(8,10)).isIn(6)); //=> returns false

    }
}