This question already has answers here:
Is it possible in Java to access private fields via reflection [duplicate]

(3个答案)


4年前关闭。




我的模板打开时显示选项菜单,用户输入1-3之间的内容以选择三个选项之一。

当用户选择选项1时,它要求他们输入数字teamNumber。必须实例化类Team,然后将其写入数组列表。

如果numberList中至少有一个数字,则用户可以选择选项2。它要求他们输入数组列表中的任何数字并进行搜索。如果找到他们输入的数字,则输入String teamMemberFirstName和char firstInitialLastName。然后它将输入内容写入另一个类TeamMember中的私有arraylist中。

一旦他们在选项1和2中输入了信息,他们就可以选择选项3。它允许您根据输入的团队编号来打印输入名称的列表。

我不确定在选项3中如何从TeamMemberteamList调用私有数组列表。有关如何进行此步骤的任何指导?我的代码如下。

主班:
public class Main {
    public static void main(String[] args) {
                        int choosing;
    Scanner scan = new Scanner(System.in);
String input;
int teamNumber;
boolean stayInLoop;
ArrayList<Team> numberList = new ArrayList<Team>();
do {
stayInLoop = true;
System.out.println("1. Add a new team");
System.out.println("2. Add a new team member");
System.out.println("3. View teams");
input = scan.nextLine();

if (input.equals("1")) {
  System.out.println("Enter a team number:");
  teamNumber = scan.nextInt();
  scan.nextLine();
  Team addTeam = new Team(teamNumber);
  numberList.add(addTeam);
}

if (input.equals("2")){
boolean foundIt = false;
boolean valid = true;
System.out.println("Team number:");
teamNumber = scan.nextInt();
scan.nextLine();
  for (int a = 0; a < numberList.size() && foundIt == false; a++){
  Team addTeam = numberList.get(a);
    if (addTeam.findTeam() == teamNumber) {
    foundIt = true;
    System.out.println("Enter first name of team member:");
    String teamMemberFirstName = scan.nextLine();
    System.out.println("Enter first initial of last name:");
    char firstInitialLastName = scan.nextLine().charAt(0);
    TeamMember inputTeamMember = new TeamMember(teamMemberFirstName, firstInitialLastName);
    inputTeamMember.addMember(inputTeamMember, valid = true);
    System.out.println("Success!");
    }
  }
  if (foundIt == false) {
  System.out.println("Try again.");
  }
}

if (input.equals("3")){
  for (int a = 0; a < numberList.size(); a++) {
  Team addTeam = numberList.get(a);
  //Not sure what to put where there are ????'s - I tried a few ideas and stuff I found online, but nothing worked
  //I assume I call the method/class here????
  System.out.println("Team: " + addTeam.findTeam() + " Members: " +
  "I will put the member called from the arraylist here????");
  }
}
}while (stayInLoop == true;)
}}

TeamMember类:
public class TeamMember {

private final String teamMemberFirstName;
private final char firstInitialLastName;
private ArrayList<TeamMember> teamList = new ArrayList<>();

public TeamMember(String teamMemberFirstName, char firstInitialLastName) {
  this.teamMemberFirstName = teamMemberFirstName;
  this.firstInitialLastName = firstInitialLastName;
}
public int addMember(TeamMember member, boolean valid) {
  valid = teamList.add(member);
  return teamList.size();
}
}

最佳答案

在公共类中,可以使用公共方法返回private对象。这似乎是该项目中最简单的方法。向您的TeamMember类添加一个新方法,并使其返回teamList:

//在分配私有变量之后的任何位置,在TeamMember类内部

public static ArrayList show(){
//the static keyword, in short, will make the method callable without a class instance.
    return teamList;
}

由于TeamMember方法show()现在是静态的,因此您应该能够简单地调用TeamMember.show()并获取ArrayList。

重要说明:为了使其正常工作,还必须将私有arraylist设为静态。静态对象不能调用非静态对象。

这将把它变成private static ArrayList<TeamMember> teamList = new ArrayList<>();
在Main类中,就像我上面所说的,只需调用TeamMember.show()。您无需创建实例。

07-26 09:33