在类中调用方法

时间:2013-04-14 20:45:45

标签: class methods

我有一个包含类的程序,然后在该类中有另一个类,在该类中有一个方法。我试图在类中调用该方法。 基本上:方法属于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()。

2 个答案:

答案 0 :(得分:0)

调用A需要一个像getB()这样的getter方法,然后你可以使用B实例的方法。或者你在A getLibraryDataInB()中创建一个推迟方法,或者你只是重写你的代码,如果必须从外面访问它们,就不要喜欢盒装类。

答案 1 :(得分:0)

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