1

我有一个程序,里面有一个类,然后在那个类中有另一个类,在那个类中有一个方法。我正在尝试在类中调用该方法。基本上:方法在 B 类中,而 B 类在 A 类中。

Class A
    Class B
        Method

B类:

class MyTableModel extends AbstractTableModel
{
    public void getLibraryData () throws IOException
    {
        BufferedReader reader = new BufferedReader (new FileReader ("" + username [0] + ".txt"));
        BufferedWriter writer = new BufferedWriter (new FileWriter ("" + username [0] + ".txt", true));
        String line = null;
        int count = 0;

        while ((line = reader.readLine ()) != null)
        {
            songs [count] = line;
            artists [count] = reader.readLine ();
            categoryA [count] = reader.readLine ();
            count++;
        }
        data = new Object [count] [];
        for (int i = 0 ; i < count ; i++)
        {
            Object[] row = data [i] = new Object [3];
            row [0] = songs [i];
            row [1] = artists [i];
            row [2] = categoryA [i];
        }
    }
    private String[] columnNames = {"Song",
        "Artist",
        "Category", };

    private Object[] [] data = {
        };

    public int getColumnCount ()
    {
        return columnNames.length;
    }

    public int getRowCount ()
    {
        return data.length;
    }

    public String getColumnName (int col)
    {
        return columnNames [col];
    }

    public Object getValueAt (int row, int col)
    {
        return data [row] [col];
    }

    public Class getColumnClass (int c)
    {
        return getValueAt (0, c).getClass ();
    }
}

我正在尝试调用 getLibraryData()。

4

2 回答 2

0

调用 A 需要一个 getter 方法,就像getB()你可以调用 B 实例的方法一样。或者你在 A like 中创建一个 deferral 方法getLibraryDataInB(),或者你只是重写你的代码并且没有像这样的盒装类,如果它们必须从外部访问的话。

于 2013-04-14T20:52:33.970 回答
0

您必须创建内部类的实例并调用该方法。干得好。 http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

于 2014-09-22T04:35:36.903 回答