如何在Java中使用私有方法

发布于 2024-12-08 18:55:07 字数 444 浏览 1 评论 0原文

我得到一个具有私有方法 setCoors(int x, int y) 的类。该类的构造函数中也包含 setCoors。在另一个类中,我想要一个调用 setCoors 的方法 setLocation。这可能吗?

新问题:

如果我不允许将该方法设置为公共,这可能吗?

public class Coordinate{
    public Coordinate(int a, int b){
        setCoors(a,b)
    }
    private void setCoords(int x, int y)
}

public class Location{
    private Coordinate  loc;
    public void setLocation(int a, int b)
        loc = new Coordinate(a,b)
}

I am given a class that has a private method say setCoors(int x, int y). The constructor of that class has the setCoors in it too. In a different class, I want to have a method setLocation which calls setCoors. Is this possible?

New Question:

If I am not allowed to set the method to public, is this possible?

public class Coordinate{
    public Coordinate(int a, int b){
        setCoors(a,b)
    }
    private void setCoords(int x, int y)
}

public class Location{
    private Coordinate  loc;
    public void setLocation(int a, int b)
        loc = new Coordinate(a,b)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

琉璃梦幻 2024-12-15 18:55:07

不,private 意味着该方法只能在定义它的类内部调用。您可能希望让 setLocation 创建 setCoords 所在类的新实例,或者更改 setCoords 的可见性。

编辑:您发布的代码将起作用。请注意,Location 类的任何实例都将绑定到其自己的 Cooperative 对象。如果您在代码中的其他位置创建一个新的坐标对象,您将无法修改其内部状态。换句话说,该行将

Coordinate myCoord = new Coordinate(4, 5);

创建对象 myCoord,该对象将永远具有坐标 45

No, private means the method can only be called inside of the Class in which it is defined. You will probably want to have setLocation create a new instance of the class setCoords resides in, or change the visibility on setCoords.

EDIT: The code you have posted will work. Just be aware that any instance of the Location class will be bound to its own Coordinate object. If you create a new Coordinate object somewhere else in your code, you will be unable to modify its internal state. In other words, the line

Coordinate myCoord = new Coordinate(4, 5);

will create the object myCoord which will forever have the coordinates 4 and 5.

帅冕 2024-12-15 18:55:07

最好和最有帮助的答案取决于问题的上下文,我认为这并不完全显而易见。

如果问题是关于 private 的预期含义的新手问题,那么答案“否”是完全合适的。即:

  • A 的私有成员只能在 A 类包中访问
  • A 的私有成员只能在 A 包中的类中访问
  • A 的受保护成员只能在 A 包和 A 的子类中访问 A
  • 的公共成员可以在任何地方访问A 可见。

现在,如果,好吧,也许这是一个延伸(谢谢布莱恩:)),这个问题来自一个更“高级”的上下文,人们正在考虑“我知道私有意味着private 但是有语言漏洞吗”,那么,嗯,有这样的漏洞。它是这样的:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

class C {
    private int x = 10;
    private void hello() {System.out.println("Well hello there");}
}

public class PrivateAccessDemo {
    public static void main(String[] args) throws Exception {
        C c = new C();
        List<Field> fields = Arrays.asList(C.class.getDeclaredFields());
        for (Field f: fields) {
            f.setAccessible(true);
            System.out.println(f.getName() + " = " + f.get(c));
        }
        List<Method> methods = Arrays.asList(C.class.getDeclaredMethods());
        for (Method m: methods) {
            m.setAccessible(true);
            m.invoke(c);
        }
    }
}

输出:

x = 10
Well hello there

当然,这确实不是应用程序程序员会做的事情。但事实上,这样的事情可以做到是值得知道的,而不是应该被忽视的事情。无论如何,恕我直言。

The best and most helpful answer depends on the context of the question, which is, I believe, not completely obvious.

If the question was a novice question about the intended meaning of private, then the answer "no" is completely appropriate. That is:

  • private members of A are accessible only within class A
  • package-private members of A are accessible only within classes in A's package
  • protected members of A are accessible only within classes in A's package and subclasses of A
  • public members of A are accessible anywhere A is visible.

Now, if, and okay maybe this is a stretch (thank you Brian :) ), that the question came from a more "advanced" context where one is looking at the question of "I know private means private but is there a language loophole", then, well, there is such a loophole. It goes like this:

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

class C {
    private int x = 10;
    private void hello() {System.out.println("Well hello there");}
}

public class PrivateAccessDemo {
    public static void main(String[] args) throws Exception {
        C c = new C();
        List<Field> fields = Arrays.asList(C.class.getDeclaredFields());
        for (Field f: fields) {
            f.setAccessible(true);
            System.out.println(f.getName() + " = " + f.get(c));
        }
        List<Method> methods = Arrays.asList(C.class.getDeclaredMethods());
        for (Method m: methods) {
            m.setAccessible(true);
            m.invoke(c);
        }
    }
}

Output:

x = 10
Well hello there

Of course, this really isn't something that application programmers would ever do. But the fact that such a thing can be done is worthwhile to know, and not something that should be ignored. IMHO anyway.

哭了丶谁疼 2024-12-15 18:55:07

private 表示它是 private

如果您希望其他类调用它,也许您不应该将其设为私有?

private means it's private

If you want other classes to call it, maybe you shouldn't make it private?

落在眉间の轻吻 2024-12-15 18:55:07

没有 private 方法不能在定义它们的类之外访问

No private methods can't be accessed outside the class in which they are defined

半岛未凉 2024-12-15 18:55:07

孩子做作业:答案是否定的。一个人需要一些疯狂的工作来完成他的工作:答案是肯定的。更重要的是,您的 setCoors 方法不应采用 int 参数。它应该需要两个 SilverBullet 对象。

Kid-doing-homework: the answer is no. Guy-requiring-some-crazy-work-around-for-his-job: the answer is yes. Far more importantly though, Your setCoors method should not take int arguments. It should take two SilverBullet objects.

分開簡單 2024-12-15 18:55:07

private 表示您只能在定义的类内部访问它。

private means you can only access it inside the class defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文