开发者

java return from private method to public

开发者 https://www.devze.com 2023-04-09 03:47 出处:网络
I have a public method and a private method. they are both supposed to return int values. The private method is the one that does all the work and the public is the one that is called from the main pr

I have a public method and a private method. they are both supposed to return int values. The private method is the one that does all the work and the public is the one that is called from the main program. How can I return the results returned from the private method by the public method?

its like this

public int longer()
{
  longer(a.length);
}

private int longer(int n)
{
  int index

  //find largest index recursively 
  //make recursive call longer(n-1)
  return index;
}

I want to pass it up to the public method and then return it from there. Would I just return it from the public method by saying return long开发者_运维技巧er.index; or something along those lines?

i guess i should clarify. n isnt index. idnex is being calculated based on whats being passed into the method. the public and the private is because its going to be a recursive method. i'll edit what i posted above to make itm ore accurate of what im trying to do. passing in an array and recursively working on it.


public int longer()
{
    return longerInternal(a.length);
}

private int longerInternal(int n)
{
    int index

    //find largest index recursively 
    //make recursive call longer(n-1)
    return index;
}

From your public method, you can call down into the private method. I renamed the private method so that there was not a naming collision for your methods. A simple implementation should look something like this:

public class MyClass {
    private int[] a;

    public MyClass(int[] _a) {
        a = _a;
    }

    public int longer()
    {
        return longerInternal(a.length);
    }

    private int longerInternal(int n)
    {
        int index;
        //do recursive call
        return index;
    }
}

And it can be called like this:

MyClass myClass = new MyClass(new int[]{1,2,3,4,5,10});
int result = myClass.longer();


First, you probably need better function names.

You'd call your public function getLonger(int n) and then pass it to your private longer(int n) function. When this function is done, it will return to getLonger(int n) and then back to the caller.


You mentioned in an answer to a comment that the "caller does not need to have access to all internal workings of a class."

To me that suggests that you want to use an interface.

Create an interface that describes the class that will contain that secret algorithm:

package com.stevej;

public interface Longer {

  public int longer();

}

Implement that interface using your secret algorithm:

package com.stevej;

public class LongerImpl implements Longer {

  private int longer(int n){
    return 0; // whatever
  }

  @Override
  public int longer() {
    return longer(5); // whatever
  }

}

Now the caller only creates objects using the interface definition, guaranteeing that there are no exposed methods that he can access by accident. That implementation is hooked to that interface-defined object:

package com.stevej;

public class LongerProcessor {

  Longer longerImpl = new LongerImpl();

  public LongerProcessor() {
    super();
  }

  public int longer() {
    return longerImpl.longer();
  }

}

Now you can rewrite the implementation of Longer as often as you like. As long as the interface definition never changes, the caller (LongerProcessor) will never have a problem. Heck, you could have two or more different implementations (LongerImplRecursive, LongerImplBruteForce, and so on), each implementing Longer, and all in use in different places in the same program:

package com.stevej;

public class LongerProcessor {

  Longer longerImpl;

  public LongerProcessor(boolean useRecursive) {
    super();

    if (useRecursive){
      longerImpl = new LongerImplRecursive();
    }else{
      longerImpl = new LongerImplBruteForce();
    }
  }

  public int longer() {
    return longerImpl.longer();
  }

}

How cool is that? Since you tagged this question as "homework", I'm wondering if the problem is supposed to engage you to think about separating the contract (interface) from the implementation (implementing class).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号