简体   繁体   English

实例方法是否需要访问 java 中的实例变量?

[英]Is it necessary for instance methods to access instance variable in java?

public class Main {

    void sum(int a, int b) {
        int c = a + b;
        System.out.println(c);
    }

    public static void main(String args[]) {
        Main ob = new Main();
        ob.sum(10, 125);
    }
}

In the above code there is no instance variable, but I have read that if a method is an instance method it should access instance variable.在上面的代码中没有实例变量,但我读到如果一个方法是一个实例方法,它应该访问实例变量。 So is 'sum' an instance method?那么'sum'是一个实例方法吗?

sum is an instance method here, because it's not static and requires an instance of the object. sum在这里是一个实例方法,因为它不是static并且需要 object 的实例。 You create your instance here:你在这里创建你的实例:

Main ob = new Main();

In this particular case sum could indeed be made static , slightly simplifying the code by not requiring an instance.在这种特殊情况下, sum确实可以static ,通过不需要实例稍微简化了代码。

I have read that if a method is an instance method it should access instance variable我已经读过,如果一个方法是一个实例方法,它应该访问实例变量

I suspect what you were reading is suggesting that if a method doesn't interact with the instance at all then it probably should be static .怀疑您正在阅读的内容表明,如果一个方法根本不与实例交互,那么它可能应该是static There may be mention of the term "pure function" in the text somewhere.文中某处可能会提到“纯函数”一词。

I wouldn't go so far as to say that all potentially static methods everywhere should be made static as a universal rule.到目前为止,我不会 go 说所有可能static方法应该被static作为通用规则。 It really comes down to the semantics of the object itself.它实际上归结为 object 本身的语义。 Since the object you have here has very little semantic context, this one tiny example could easily go either way.由于您在这里拥有的 object几乎没有语义上下文,因此这个小例子可以很容易地 go 任何一种方式。

But suppose you expanded your object to also include methods for subtract , multiply , divide , etc. As the object expands, suppose one or more of those additional methods did use instance variables.但是假设您扩展了 object 以还包括subtractmultiplydivide等方法。随着 object 的扩展,假设这些附加方法中的一个或多个确实使用了实例变量。 It would be a jarring experience to have an object with multiple semantically-similar methods, some of which are static and some of which are not.如果 object 具有多种语义相似的方法,其中一些是static而一些不是,这将是一种不和谐的体验。

Rather than focus on any particular rule that someone gives you, focus on what you are intending to build as your object.与其关注某人给您的任何特定规则,不如关注您打算构建的 object。 If you think that it should be static , make it as such.如果你认为它应该static ,那就这样吧。 If you feel that it shouldn't, then don't.如果你觉得不应该,那就不要。 If you're unsure, then as an exercise implement both and see which you prefer in that particular case.如果您不确定,那么作为练习实施两者并查看在特定情况下您更喜欢哪个。

Looking to the future plans for what you're intending to build is important, because the more code you have relying on your object throughout the application the harder it will be to change from static to instance or vice-versa.展望您打算构建的未来计划很重要,因为在整个应用程序中依赖 object 的代码越多,从static更改为实例的难度就越大,反之亦然。

Yes, 'sum' an instance method because any non-static method is an instance method.是的, “求和”一个实例方法,因为任何非静态方法都是实例方法。 Moreover, It is not necessary that an instance method should access instance variable.此外,实例方法不必访问实例变量。 The correct statement is "if a method is an instance method it can access instance variable.正确的说法是“如果一个方法是一个实例方法,它可以访问实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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