这个私有方法的作用域是如何工作的?

huangapple go评论62阅读模式
英文:

How does the scope of this private method work?

问题

public class Problem13 {
private Tuple costs;

private Tuple<String> names;

private Tuple<Integer> result;

private int budget;

private int minDelta, minCost, totalCost;

public void process(String fileName) {
    if (!read(fileName))
        return;

    if (budget >= totalCost) {
        System.out.println("You can buy all items");

        return;
    }

    if (budget < minCost) {
        System.out.println("You cannot buy items");

        return;
    }

    minDelta = -1;

    int n = costs.getLength();

    Set<Integer> interval = new IntegerInterval(0, n - 1);

    for (int k = n - 1; k > 0; --k) {
        Combinations<Integer> combinations = new Combinations<Integer>(interval, k);

        combinations.produce((tuple) -> !check(tuple));

        if (minDelta == 0)
            break;
    }

    if (result == null)
        System.out.println("No solution found");
    else {
        int k = result.getLength();

        for (int j = 0; j < k; ++j)
            System.out.printf("%s ", names.get(result.get(j)));

        System.out.printf("(%d)\n", minDelta);
    }
}

public static void main(String[] args) {
    new Problem13().process("data/input13.txt");
}

private boolean check(Tuple<Integer> tuple) {
    int k = tuple.getLength();

    int currentCost = 0;

    for (int i = 0; i < k; ++i) {
        int j = tuple.get(i);

        currentCost += costs.get(j);

        if (currentCost > budget)
            return false;
    }

    int d = budget - currentCost;

    if (minDelta < 0 || d < minDelta) {
        minDelta = d;

        result = new ArrayTuple<>(k);

        for (int i = 0; i < k; ++i)
            result.set(i, tuple.get(i));
    }

    return minDelta == 0;
}

}

英文:

My question is: how can I see the Tuple result in the process method if it was created in the check method? How am I able to use it there, if it was created in a private method?

 public class Problem13 {
	private Tuple&lt;Integer&gt; costs;

	private Tuple&lt;String&gt; names;

	private Tuple&lt;Integer&gt; result;

	private int budget;

	private int minDelta, minCost, totalCost;

	public void process(String fileName) {
		if (!read(fileName))
			return;

		if (budget &gt;= totalCost) {
			System.out.println(&quot;You can buy all items&quot;);

			return;
		}

		if (budget &lt; minCost) {
			System.out.println(&quot;You cannot buy items&quot;);

			return;
		}

		minDelta = -1;

		int n = costs.getLength();

		Set&lt;Integer&gt; interval = new IntegerInterval(0, n - 1);

		for (int k = n - 1; k &gt; 0; --k) {
			Combinations&lt;Integer&gt; combinations = new Combinations&lt;Integer&gt;(interval, k);

			combinations.produce((tuple) -&gt; !check(tuple));

			if (minDelta == 0)
				break;
		}

		if (result == null)
			System.out.println(&quot;No solution found&quot;);
		else {
			int k = result.getLength();

			for (int j = 0; j &lt; k; ++j)
				System.out.printf(&quot;%s &quot;, names.get(result.get(j)));

			System.out.printf(&quot;(%d)\n&quot;, minDelta);
		}
	}

	public static void main(String[] args) {
		new Problem13().process(&quot;data/input13.txt&quot;);
	}

	private boolean check(Tuple&lt;Integer&gt; tuple) {
		int k = tuple.getLength();

		int currentCost = 0;

		for (int i = 0; i &lt; k; ++i) {
			int j = tuple.get(i);

			currentCost += costs.get(j);

			if (currentCost &gt; budget)
				return false;
		}

		int d = budget - currentCost;

		if (minDelta &lt; 0 || d &lt; minDelta) {
			minDelta = d;

			result = new ArrayTuple&lt;&gt;(k);

			for (int i = 0; i &lt; k; ++i)
				result.set(i, tuple.get(i));
		}

		return minDelta == 0;
	}

答案1

得分: 0

private 意味着对于这个类来说是私有的。所以 Problem13 可以访问该类中定义的任何内容,无论是 privatepublicprotected 还是 package private

此外,方法的访问修饰符只影响谁可以调用它,而不影响结果可以在哪里看到。例如,如果 result 被定义为一个 public 字段,任何类(不仅仅是 Problem13)都可以看到它。

在互联网上有很多关于访问修饰符的良好解释。这里是一个例子

英文:

private means private to the class. So Problem13 can see anything defined in that class, whether private, public, protected or package private.

Also, the access modifier of the method only affects who can call it, not where the results can be seen. For instance, if result was defined as a public field, any class (not just Problem13) could see it.

You can find many good breakdowns of access modifiers out there on the Interwebs. Here's one.

huangapple
  • 本文由 发表于 2020年8月30日 09:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定