了解Java实例和静态关键字

发布于 2025-01-18 05:35:40 字数 650 浏览 0 评论 0原文

我可以做什么来使用 Program 类上的 getTotalScore 方法来获取我的 scores ArrayList 的总和,为什么我必须这样做?我想明白。请注意,我不想使该方法静态。

import java.util.*;

public class Program {

    public int getTotalScore(ArrayList<int> v) {
        
        return total;
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<int> scores = new ArrayList<>();


}
}

我找不到一篇文章可以帮助我清楚地了解发生了什么,请帮忙。我在某处收到这样的评论:

请注意,scores 对象不是静态的,因此您必须创建一个实例 Program 类,以便使用分数 ArrayListgetTotalScore 方法。

what can I do to use the getTotalScoremethod on the Program class to get the total of my scores ArrayList, and why must I do that? I wanna understand. Please note I don't wanna make the method static.

import java.util.*;

public class Program {

    public int getTotalScore(ArrayList<int> v) {
        
        return total;
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<int> scores = new ArrayList<>();


}
}

I can't find an article to help me understand clearly what is up, please help. I got this comment somewhere:

Note that the scores object is not static, so you will have to make an instance of the
Program class in order to use the scores ArrayList and the getTotalScore method.

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

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

发布评论

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

评论(3

猫性小仙女 2025-01-25 05:35:40

如果要使用类的非静态成员,则必须实例化该类。所以在这种情况下

new Program().getTotalScore(scores)

,创建 Program 类的实例是非常奇怪的。将方法 getTotalScore 移至新类(或使其静态)。

为了计算总数,请查看如何对整数数组进行“求和”。

If you want to use a non-static member of a class, you must instantiate the class. So in this case

new Program().getTotalScore(scores)

This is very weird, to create an instance of the Program class. Move the method getTotalScore to a new class instead (or make it static).

For calculating the total, look up how to “sum” an array of integers.

知足的幸福 2025-01-25 05:35:40

静态成员属于类,而不是实例。

为了使用getTotalsCore MAIN 方法中的方法,您要么必须创建一个program> program>的对象,要么制作<代码> getTotalsCore静态方法。

以下是两个示例:

  1. 创建program的实例
public class Program {

    public int getTotalScore(ArrayList<Integer> v) {
        int total = 0;
        for(int i = 0; i < v.size(); i++){
            total += v.get(i);
        }
        return total;
    }


    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<Integer>();
        Program program = new Program();
        program.getTotalScore(scores);
    }
}
  1. 制作getTotalsCore静态方法
public class Program {

    public static int getTotalScore(ArrayList<Integer> v) {
        int total = 0;
        for(int i = 0; i < v.size(); i++){
            total += v.get(i);
        }
        return total;
    }


    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<Integer>();
        Program.getTotalScore(scores);
    }

static members belong to a class, not an instance.

In order to use getTotalScore method inside your main method, you'll either have to create an object that is instance of Program, or make getTotalScore a static method.

Here are two examples:

  1. Creating an instance of Program
public class Program {

    public int getTotalScore(ArrayList<Integer> v) {
        int total = 0;
        for(int i = 0; i < v.size(); i++){
            total += v.get(i);
        }
        return total;
    }


    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<Integer>();
        Program program = new Program();
        program.getTotalScore(scores);
    }
}
  1. Making getTotalScore a static method
public class Program {

    public static int getTotalScore(ArrayList<Integer> v) {
        int total = 0;
        for(int i = 0; i < v.size(); i++){
            total += v.get(i);
        }
        return total;
    }


    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<Integer>();
        Program.getTotalScore(scores);
    }
寄与心 2025-01-25 05:35:40
public class Q71700414 {
public int getTotalScore(ArrayList<Integer>  v) {
    int total=0;;
    for(int i=0;i<v.size();i++) {
        total+=v.get(i);
    }
    return total;
}
public static void main(String[] args) {
    ArrayList<Integer>v=new ArrayList<Integer>();
    for(int i=0;i<10;i++)
        v.add(i+1);
    Q71700414 obj=new Q71700414();
    System.out.println("Total is "+obj.getTotalScore(v));
    System.out.println("Total is "+new Q71700414().getTotalScore(v));
}

这里

我包括两种类型的 instatiate 对象(在本例中是调用方法)。

public class Q71700414 {
public int getTotalScore(ArrayList<Integer>  v) {
    int total=0;;
    for(int i=0;i<v.size();i++) {
        total+=v.get(i);
    }
    return total;
}
public static void main(String[] args) {
    ArrayList<Integer>v=new ArrayList<Integer>();
    for(int i=0;i<10;i++)
        v.add(i+1);
    Q71700414 obj=new Q71700414();
    System.out.println("Total is "+obj.getTotalScore(v));
    System.out.println("Total is "+new Q71700414().getTotalScore(v));
}

}

Here I have include two type of instatiate object(in this case calling method).

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