简体   繁体   中英

How do I create an object referencing an instantiated class object inside a method that didn't pass the object as an argument?

synchronized void drop(Board board) {
    int[][] a = getArray();
    int[][] b = board.getArray();

    //I don't have currentObject here... what do I need to write?                
    if (Board.goDown(currentX, currentY, b, a, board, currentObject)) {
        currentY++;
        updateXY();
    }
}

The method call is currentObject.drop(board) , but we can't pass in currentObject as a variable.

Is there any easy way of doing this or I just have to rewrite the entire code?

You can use this keyword .

"this" is a reference to the current object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM