简体   繁体   English

Java-将实例从一个类传递到另一个类

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

Say that I have 3 classes, Class A, Class B, and Class C. I'm using class A & B as a static class, and Class C as a object. 假设我有3个类,即A类,B类和C类。我将A和B类用作静态类,并将C类用作对象。

Class C looks something like this: C类看起来像这样:

public class C {

    int someInformation;

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

In Class B, I create a instance of this object, and attempt to pass it to a function in Class A. 在类B中,我创建此对象的实例,然后尝试将其传递给类A中的函数。

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

If in doSomething, I try to use 如果在doSomething中,我尝试使用

classC.setInfo(integerInputed);

in ClassA, I get a nullPointerException. 在ClassA中,我得到一个nullPointerException。 What am I doing wrong? 我究竟做错了什么?

In these classes, I am trying to simulate the building of a battleship board. 在这些课程中,我试图模拟战舰主板的建造。

The real Class C is as follows: 真正的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;
            }
        }
    }
}           
}

The real Class A is as follows: 真正的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;
}
}

In line 8 of the setShips() method, I input the ships object. 在setShips()方法的第8行中,我输入了ships对象。

The real Class B: I get the error in line 19 of this class. 真正的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