2015-09-26 155 views
-1

有像的Java實施的接口類型的方法在實現接口

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))添加基於方法Range newRange(1,5),範圍從1到5加上6到8。如果值在此範圍內,則返回boolean isIn(int value)int min()返回範圍內的最小值。我如何使用class作爲參考類型來實現這些方法?通過傳遞一個對象?我newRange

public Range newRange(int from,int to){ 
    RangeImplem impIns = new RangeImplem(); 
    impIns.from = from; 
    impIns.to = to; 
    return impIns; 
} 

我對這個問題,不知道和班級爲引用類型有點混亂。謝謝。

+0

要實現它,你不需要。要使用它,你需要...... –

+0

'newRange'不應該是接口的一種方法。這是(通常)實現'Range'接口的類的構造函數。在你的情況下,這將是'RangeImplem'類。 – Andreas

回答

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; 
    } 
} 
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 

    } 
}