java - 为什么这个方法必须是静态的(Java)?

标签 java static

对于一些背景知识,我目前正在阅读书中的第 8 章,我们已经讨论完了数组列表、数组、if 语句、循环等。现在这本书的这一部分讨论了引用调用、值和其他一些非常简洁的内容起初我觉得有些奇怪。我读过 What situation to use static以及其他一些 SO 问题,也学到了很多东西。

考虑我的书中给出的以下示例(在许多示例中)

There is another reason why static methods are sometimes necessary. If a method manipulates a class that you do not own, you cannot add it to that class. Consider a method that computes the area of a rectangle. The Rectangle class in the standard library has no such feature, and we cannot modify that class. A static method solves this problem:

public class Geometry
{
  public static double area(Rectangle rect)
  {
    return rect.getWidth() * rect.getHeight();
  }
// More geometry methods can be added here.
}

Now we can tell you why the main method is static. When the program starts, there aren’t any objects. Therefore, the first method in the program must be a static method.

好吧,那很酷,到目前为止我只是盲目地输入 public在我所有的方法之前,所以很高兴知道这一点。但是下一页的评论小问题引起了我的注意

The following method computes the average of an array list of numbers:

public static double average(ArrayList<Double> values)

Why must it be a static method?

在这里我想等一下。我很确定我之前没有使用 static 就做到了这一点。所以我再次尝试这样做,并且很容易地想出了以下内容

import java.util.ArrayList;
class ArrList
{
    private double sum;

    public ArrList()
    {
        sum = 0;
    }

    public double average(ArrayList <Double> values)
    {
        for(int i = 0; i < values.size() ; i++)
        {
            sum+=values.get(i);
        }

        return sum / values.size();

    }
}

public class Average
{
    public static void main(String [] args)
    {
        ArrList arrListObj = new ArrList();
        ArrayList<Double> testArrList = new ArrayList<Double>();
        testArrList.add(10.0);
        testArrList.add(50.0);
        testArrList.add(20.0);
        testArrList.add(20.0);
        System.out.println(arrListObj.average(testArrList));

    }
}

TLDR

为什么我的书这么说public static double average(ArrayList<Double> values)需要静态吗?

尝试使用静态

public class Average
{
    public static void main(String [] args)
    {
        ArrayList<Double> testArrList = new ArrayList<Double>();
        ArrayList<Double> testArrListTwo = new ArrayList<Double>();
        testArrList.add(10.0);
        testArrList.add(50.0);
        testArrList.add(20.0);
        testArrList.add(20.0);
        testArrListTwo.add(20.0);
        testArrListTwo.add(20.0);
        testArrListTwo.add(20.0);
        System.out.println(ArrList.average(testArrList));
        System.out.println(ArrList.average(testArrListTwo)); // we don't get 20, we get 53.3333!


    }
}

最佳答案

事实并非如此。

唯一需要静态的方法是初始main()方法。作为程序员,任何事情都取决于你来决定什么对你的设计有意义。

staticpublic 访问器(正如您所提到的)无关,并且与正在执行的技术操作无关。它与操作的语义和保存它的类有关。

实例(非静态)方法存在于类的特定实例上。从语义上讲,它应该执行与该特定实例相关的操作。静态方法通常存在于类中,并且更具概念性。它不会对特定实例执行任何操作(当然,除非它提供了某个实例作为方法参数)。

所以你真的只需要问自己操作的语义。您是否需要对象的new实例来执行操作?或者该操作是否应该在没有实例的情况下可用?这取决于操作、对象所代表的内容等。

关于java - 为什么这个方法必须是静态的(Java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902858/

相关文章:

java - 将字符串文字拆分成多行

java - 如何在 hibernate/jpa 中保存一对多?

java - <fmt :setLocale value ="${param.locale }" scope ="session"/>tag is not used

c++ - 在现代 C++ 游戏引擎中组织全局系统访问

c - 关于C中static关键字的两个问题

java - java中元素未写入数组

java - 为 vertx 项目生成 OpenAPI 规范

c# - CLR 如何处理静态类?

c++ - "static functions with block scope are illegal"错误取决于初始化样式?

c# - C# 中的 const 和 static 有什么区别?