簡體   English   中英

如何創建內部類的對象數組?

[英]How to create array of objects of an inner class?

我有一個叫做table的類,在它里面有一個稱為Cell的嵌套類。 我將table分成幾個單元格。 我想創建一個稱為Cell的嵌套類的數組,我將自己介紹給一些說明性教程,以了解如何從嵌套/內部類實例化一個對象,但是沒有一個實例提供了如何實例化一個數組的實例。嵌套類。 下面是我的嘗試,日食突出顯示cell = mTable.Cell[27]; 帶有紅色花體

代碼:Table_Class

// declaration
private Table mTable;
private Table.Cell []cell;
...
...
...
package com.example.kotschiena02;

public class Table {

    private int table_X1;
    private int table_Y1;
    private int table_X2;
    private int table_Y2;

    public Table(int x1, int y1, int x2, int y2) {
        this.table_X1 = x1;
        this.table_Y1 = y1;
        this.table_X2 = x2;
        this.table_Y2 = y2;
    }

    public int getTableWidth() {
        return (this.table_X2 - this.table_X1);
    }

    public int getTableHeight() {
        return (this.table_Y2 - this.table_Y1);
    }

    public int getTable_X1() {
        return this.table_X1;
    }

    public int getTable_Y1() {
        return this.table_Y1;
    }

    public int getTable_X2() {
        return this.table_X2;
    }

    public int getTable_Y2() {
        return this.table_Y2;
    }

    private class Cell {
        private int cell_ID;
        private int cell_X1;
        private int cell_Y1;
        private int cell_X2;
        private int cell_Y2;
        private boolean occupancyState;

        public Cell (int id, int x1, int y1, int x2, int y2, boolean occupancyState) {
            this.cell_ID = id;
            this.cell_X1 = x1;
            this.cell_Y1 = y1;
            this.cell_X2 = x2;
            this.cell_Y2 = y2;
            this.occupancyState = occupancyState;
        }

        public int getCell_X1() {
            return this.cell_X1;
        }

        public int getCell_Y1() {
            return this.cell_Y1;
        }

        public int getCell_X2() {
            return this.cell_X2;
        }

        public int getCell_Y2() {
            return this.cell_Y2;
        }

        public int getCell_ID() {
            return this.cell_ID;
        }

        public void setOccupancyState(boolean state) {
            this.occupancyState = state;
        }

        public boolean getOccupancyState() {
            return this.occupancyState;
        }
    }
}

代碼:SetupTable()

private void setupTable() {
    // TODO Auto-generated method stub
    Log.i(TAG, "@setupTable:");

    mTable = new Table( 10, 
            ((screenHeight/2)-(2*cardHeight)), 
            (screenWidth-10), 
            ((screenHeight/2)+(2*cardHeight)) );

    cell = mTable.Cell[27];
public class Table {

//other properties
private List<Cell> cells;

class Cell {
//properties, getters and setters
}

//getters and setters
}

//Setting Up table
public void setupTable() {
// TODO Auto-generated method stub
Log.i(TAG, "@setupTable:");


Table mTable = new Table( 10, 
        ((screenHeight/2)-(2*cardHeight)), 
        (screenWidth-10), 
        ((screenHeight/2)+(2*cardHeight)) );

Table.Cell cell = mTable.getCells().get(27);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM