java—为什么使用arraylist但返回基元类型的方法要求方法名包含“static”?

bmp9r5qi  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(331)

这个问题在这里已经有答案了

“不能从静态上下文引用非静态方法”背后的原因是什么[重复](13个答案)
不能从静态上下文引用非静态变量(12个答案)
13天前关门了。
这个程序工作,但我不知道为什么方法使用 ArrayList 必须包括 static 在方法头中。仅作为背景,程序接收文本文件,读取它并将其内容保存到 ArrayList<Game> (游戏)。用户输入文件名和球队名称,程序输出该球队打了多少场比赛,赢了多少场比赛,输了多少场比赛。
它读取的.csv具有以下格式:

ENCE,Vitality,9,16
ENCE,Vitality,16,12
etc..

下面的四个方法,一个方法返回 ArrayList 另外三个回来了 int s、 如果我不输入关键字,就不会起作用 static 在方法头中。如果我只写 public ArrayList<Game> 或者 public int 对于这些方法,它们将不起作用。
是不是任何时候 Arraylist 正在传递到一个方法中,这意味着方法头必须始终包含 static ? 为什么会这样?
以下是工作代码:

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;

public class SportStatistics {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("File: ");
        String file = scan.nextLine();

        ArrayList<Game> record = getGame(file); //After method returns the list of 
        objects, it is copied over to another list.

        System.out.println("Team: ");
        String team = scan.nextLine(); 

        //These methods return how many games a team played, has won and has lost.
        int gamesPlayed = getGamesPlayed(record, team);
        int gamesWon = getGamesWon(record, team);
        int gamesLost = getGamesLost(record, team);

        System.out.println("Games: " + gamesPlayed);
        System.out.println("Wins: " + gamesWon);
        System.out.println("Losses: " + gamesLost);

    }

    //This method takes in the file name given by user and saves file details into 
    a list of objects.
  **public static**ArrayList<Game> getGame(String file){
        ArrayList<Game> games = new ArrayList<>();

        try(Scanner reader = new Scanner(Paths.get(file))){
            while(reader.hasNextLine()){
                String input = reader.nextLine();
                String[] parts = input.split(",");
                String homeTeam = parts[0];
                String visitingTeam = parts[1];
                int homePoints = Integer.valueOf(parts[2]);
                int visitingPoints = Integer.valueOf(parts[3]);

                games.add(new Game(homeTeam, visitingTeam, homePoints, 
                visitingPoints));
            }
        }
        catch(Exception e){
            System.out.println("Error: File " + file + " not found.");
        }

        return games;
    }

  **public static int**getGamesPlayed(ArrayList<Game> record, String team){
        int gamesPlayed = 0;
        for(Game game: record){
            if (game.getHomeTeam().equals(team) || 
            game.getVisitngTeam().equals(team)){
                gamesPlayed++;
            }
        }

        return gamesPlayed;
    }

  **public static int**getGamesWon(ArrayList<Game> record, String team){
        int gamesWon = 0;
        for(Game game: record){
            if (game.getHomeTeam().equals(team) || 
            game.getVisitngTeam().equals(team)){
                if(game.getHomeTeam().equals(team) && game.getHomePoints() > 
                game.getVisitingPoints()){
                    gamesWon++;
                }
                if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() > 
                game.getHomePoints()){
                    gamesWon++;
                }
            }
        }

        return gamesWon;
    }

  **public static int**getGamesLost(ArrayList<Game> record, String team){
        int gamesLost = 0;
        for(Game game: record){
            if (game.getHomeTeam().equals(team) || 
            game.getVisitngTeam().equals(team)){
                if(game.getHomeTeam().equals(team) && game.getHomePoints() < 
                game.getVisitingPoints()){
                    gamesLost++;
                }
                if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() < 
                game.getHomePoints()){
                    gamesLost++;
                }
            }
        }

        return gamesLost;
    }

}
mo49yndu

mo49yndu1#

错误与参数列表或返回类型无关。您正在静态上下文中使用这些方法(即,您没有在特定示例上调用它们),因此它们必须 static .

flseospp

flseospp2#

静态只是意味着它不是从类创建的对象的一部分。它独立于您创建的对象/示例。你可以说它只是与类相关/是类的一部分,而不是特性。

相关问题