簡體   English   中英

Java-將實例從一個類傳遞到另一個類

[英]Java- Pass instance from one class to another

假設我有3個類,即A類,B類和C類。我將A和B類用作靜態類,並將C類用作對象。

C類看起來像這樣:

public class C {

    int someInformation;

    public void setInfo(int newInfo){
        someInformation=newInfo;
    }
}

在類B中,我創建此對象的實例,然后嘗試將其傳遞給類A中的函數。

public void someFunction(){
        ClassC classC = new ClassC();
        ClassA.doSomething(classC, 6);
    }

如果在doSomething中,我嘗試使用

classC.setInfo(integerInputed);

在ClassA中,我得到一個nullPointerException。 我究竟做錯了什么?

在這些課程中,我試圖模擬戰艦主板的建造。

真正的C類如下:

package boardBuilder;
import java.util.Random;

public class Ship {

int[] x;
int[] y;
int shipSize;

public void setSize(int setSizeSize){
    shipSize = setSizeSize;
    x = new int[shipSize];
    y = new int[shipSize];
}

public int getSize(){
    return shipSize;
}

public int[] getX(){
    return x;
}

public int[] getY(){
    return y;
}

public void createShipForm(){
    Random rnd = new Random();
    x[0]=0;
    y[0]=0;
    int direction;
    for(int extraPlace = 1; extraPlace+1<=shipSize; extraPlace++){
        direction = Math.abs(rnd.nextInt()%4);
        if(direction==0){
            x[extraPlace]=x[extraPlace-1];
            y[extraPlace]=y[extraPlace-1]+1;
        }
        if(direction==1){
            x[extraPlace]=x[extraPlace-1]+1;
            y[extraPlace]=y[extraPlace-1];
        }
        if(direction==2){
            x[extraPlace]=x[extraPlace-1];
            y[extraPlace]=y[extraPlace-1]-1;
        }
        if(direction==3){
            x[extraPlace]=x[extraPlace-1]-1;
            y[extraPlace]=y[extraPlace-1];
        }
        for(int checkLocation=0;checkLocation<extraPlace;checkLocation++){
            if(x[extraPlace]==x[checkLocation]&&y[extraPlace]==y[checkLocation]){
                extraPlace--;
                break;
            }
        }
    }
}           
}

真正的A類如下:

package boardBuilder;

public class BoardBuilder {
    int[][] board;
    Ship[][] ships;

public void initializeBoard(int boardWidth, int boardLength, int num1, int num2, int num3, int num4, int num5, int num6){
    board = new int[boardWidth][boardLength];
    Ship[] ships1 = new Ship[num1];
    Ship[] ships2 = new Ship[num2];
    Ship[] ships3 = new Ship[num3];
    Ship[] ships4 = new Ship[num4];
    Ship[] ships5 = new Ship[num5];
    Ship[] ships6 = new Ship[num6];
    Ship[][] shipsCopy = {ships1, ships2, ships3, ships4, ships5, ships6};
    ships = shipsCopy;
}

public void setShips(){
    int count6 = ships[5].length;
    int count5 = ships[4].length;
    int count4 = ships[3].length;
    int count3 = ships[2].length;
    int count2 = ships[1].length;
    int count1 = ships[0].length;
    for(int set6 = 1; set6 <= count6; set6++){
        board = AddShip.addShip(board, 6, ships[5][set6-1]);
    }
    for(int set5 = 1; set5 <= count5; set5++){
        board = AddShip.addShip(board, 5, ships[4][set5-1]);
    }
    for(int set4 = 1; set4 <= count4; set4++){
        board = AddShip.addShip(board, 4, ships[3][set4-1]);
    }
    for(int set3 = 1; set3 <= count3; set3++){
        board = AddShip.addShip(board, 3, ships[2][set3-1]);
    }
    for(int set2 = 1; set2 <= count2; set2++){
        board = AddShip.addShip(board, 2, ships[1][set2-1]);
    }
    for(int set1 = 1; set1 <= count1; set1++){
        board = AddShip.addShip(board, 1, ships[0][set1-1]);
    }
}

public int[][] getBoard(){
    return board;
}

public Ship[][] getShips(){
    return ships;
}
}

在setShips()方法的第8行中,我輸入了ships對象。

真正的B類:在該類的第19行中出現錯誤。

package boardBuilder;

import java.util.Random;

public class AddShip {
    public static int[][] addShip(int[][] board, int size, Ship ship1){
        Ship ship = new Ship();
        ship = ship1;
        boolean shipPlaced = false;
        int[][] boardCopy = board;
        int[] shipX = new int[size];
        int[] shipY = new int[size];
        int yPosition = 0;
        int xPosition = 0;
        int width = board.length;
        int height = board[0].length;
        while(!shipPlaced){
        Random rnd = new Random();
        ship.setSize(size);
        ship.createShipForm();
        shipX = ship.getX();
        shipY = ship.getY();
        for(int placeAttempt = 1; placeAttempt <=10&!shipPlaced; placeAttempt++){
            shipPlaced=true;
            xPosition = Math.abs(rnd.nextInt()%width);
            yPosition = Math.abs(rnd.nextInt()%height);
            for(int setCoordinate = 0; setCoordinate<size;setCoordinate++){
                if(xPosition + shipX[setCoordinate]>=0&xPosition + shipX[setCoordinate]<width&yPosition + shipY[setCoordinate]>=0&yPosition + shipY[setCoordinate]<height){
                if(boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] == 0){
                    boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] = 2;
                }else{
                    shipPlaced=false;
                    break;
                }
                }else{
                    shipPlaced=false;
                    break;
                }

            }
            if(shipPlaced){
                boardCopy=surroundShip(boardCopy, shipX, shipY, xPosition, yPosition);
                break;
            }
            if(shipPlaced==false){
                boardCopy=board;
            }
        }
        }
        board = boardCopy;
        return board;
    }

    public static int[][] surroundShip(int[][] board, int[] shipX, int[] shipY, int startX, int startY){
        int shipSize = shipX.length;
        int width = board.length;
        int length = board[0].length;
        for(int coordinate = 0; coordinate<shipSize;coordinate++){
            for(int relX = -1; relX<=1; relX++){
                for(int relY = -1; relY<=1; relY++){
                    if(relX+shipX[coordinate]+startX<width&relX+shipX[coordinate]+startX>=0&relY+shipY[coordinate]+startY<length&relY+shipY[coordinate]+startY>=0){
                    if(board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]!=2){
                        board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]=5;
                    }
                    }
                }
            }
        }
        return board;
    }
}
ClassC classc = new ClassC(); ClassA.doSomething(classC, 6);

對象名稱是classc還是classC?

暫無
暫無

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

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