简体   繁体   中英

Why is this method not recursive?

I'm not quite sure how to ask this question without posting the whole code here (it's quite a bit), but I'll try my best.

I have an enum class which implements an interface. The purpose of the whole program is to represent a bunch of integer numbers in Fields. So there is a concrete class TrueField which is derived from abstract class AbstractField that has the implementation of a method called boolean sameAs(Field that) . That method also exists (it has to, because of the interface) in the enum class:

enum SimpleField implements Field{

  Empty(),Zero(0),Binary(0,1),Unsigned(Integer.MAX_VALUE);

  private Field simpleField;

  SimpleField(int... intArray){
      simpleField = new TrueField(intArray);
  }

  @Override
  public boolean sameAs(Field that){
      return that.sameAs(simpleField);
  }
}

Implementation from TrueField :

public class TrueField extends AbstractField{

private final int[] intArray;

TrueField(int... thatArray){
    intArray = thatArray;
}

@Override
public int at(int index){
    if(index<0 || index>=intArray.length){
        throw new IndexOutOfBoundsException();
    }

    return intArray[index];
}

@Override
public int length(){
    return intArray.length;
}
...

AbstractField:

public abstract class AbstractField implements Field{

@Override
public abstract int length();   


@Override
public boolean sameAs(Field that){
    if(that==null)
        throw new RuntimeException("that is null");

    boolean result = true;

    if(length()==that.length()){
        for(int i=0;i<length();i++){
            if(at(i)!=that.at(i))
                result = false;
        }
    }
    else
        result = false;

    return result;
}

@Override
public String toString(){

    String result = "";

    for(int i=0;i<length();i++){
        result += at(i);

        if(length()-i>1)
            result += ",";
    }

    return "["+result+"]";
}

}

My question is, when I do something like this in my main method:

Field sf = SimpleField.Binary;
Field sf2 = SimpleField.Binary;
Field tf = new TrueField(1,2);

System.out.println(sf.sameAs(sf2));

...obviously the method sameAs in the enum class gets called. But why isn't it calling itself again so it is recursive? As there is dynamic binding because of the interface the JVM sees that sf is the dynamic type SimpleField.Binary and the static type Field . I don't quite understand what's going on and why it isn't calling itself again. I hope I've explained my question clear enough.

It's not recursive because your sameAs method in the SimpleField enum calls the sameAs method of the parameter object, which is not a SimpleField , it's a TrueField .

So the sameAs method is being run as declared in the Field class.

On further inspection, it could be recursive, but only if the declaration in the TrueField class was recursive also, which we can see that it is not, now that this code has been added above.

The method isn't recursive because it's not calling itself .

@Override
public boolean sameAs(Field that){
    return that.sameAs(simpleField);
}

This method is using the argument to call sameAs . The argument may very well be the same enum, but it's still not calling itself.

This is an example of a simple recursive method:

public long factorial(int value) {
    return value == 0 ? 1 : factorial(value-1);
}

In this, there's no extra reference to use to call the method again; I'm just invoking it once more with a slight change in parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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