gpt4 book ai didi

java - 在Java中为类添加私有(private)字段

转载 作者:行者123 更新时间:2023-11-30 03:46:20 27 4
gpt4 key购买 nike

我有两个相关的类 SpaceInvadersApp 和 HighScoreTableModel

我需要向 HighScoreTableModel 类型的 SpaceInvadersApp 类添加一个新的私有(private)字段,并在 SpaceInvadersApp 构造函数的开头初始化该字段。

这是我的尝试,我认为我做得不正确。

public class SpaceInvadersApp extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;

private final GamePanel game;

private HighScoreTableModel highScoreTableModel; // unsure about this

final private JMenuItem menuItemGamePause;

/**
* Create new Space Invaders application.
* @throws HeadlessException
*/
public SpaceInvadersApp() throws HeadlessException {

highScoreTableModel = new HighScoreTableModel("Name", "Score"); //unsure about this

这是 HighScoreTableModel 类的相关代码

package spaceinvaders.highscores;

import java.util.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class HighScoreTableModel implements TableModel {


private List<String> col1StringList = new ArrayList<String>();
private List<Integer> col2IntegerList = new ArrayList<Integer>();
private String col1Name, col2Name;

private List<TableModelListener> listenerList = new ArrayList<TableModelListener>();

public HighScoreTableModel(String col1Name, String col2Name) {
this.col1Name = col1Name;
this.col2Name = col2Name;

最佳答案

考虑找这个类的包,需要在同一个包或者导入。

另一个细节是您的变量menuItemGamePause,它需要初始化,因为它是final

final private JMenuItem menuItemGamePause = new JMenuItem();

关于java - 在Java中为类添加私有(private)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com