简体   繁体   中英

my toString function is called without calling it

I'm very new to Java and I am making this program which randomly puts robots in a predefined arena.

I was testing my addRobot method in the main when this happened 在此处输入图片说明

Here's the code of my 2 classes:

import java.util.Scanner;
import java.util.Random;



public class RobotArena {

    private int xmax,ymax;
    Robot ro;
    RobotArena roaa;
    public static Robot rrr[];
    RobotArena (int xs, int ys, Random r){
        xmax = xs;
        ymax = ys;
        ro = new Robot(r.nextInt(xmax), r.nextInt(ymax), this); 
    }

    public void addRobot () {

    Scanner add = new Scanner(System.in); 
    System.out.print("How many robots would you like to add?");


    int numberofRobots = add.nextInt();
    rrr = new Robot[numberofRobots];
    add.close();

    for(int c=0 ;c < numberofRobots ; c++)
    {
        Random addro2 = new Random();
        int Xcoordinate = addro2.nextInt(xmax);
        int Ycoordinate = addro2.nextInt(ymax);
        rrr[c]= new Robot(Xcoordinate,Ycoordinate, null);
        System.out.println( rrr[c]);
    }

    }

import java.util.Random;

public class Robot {

    private int x, y, dx, dy, ID;
    private static int RobotID;
    private  RobotArena roA;


    Robot(){
        this(0,0, null);

    }

    Robot(int sx, int sy, RobotArena ra){
        x = sx;
        y = sy;
        dx = 1;
        dy = 1;
        roA = ra;
        ID = RobotID;
        RobotID++;


    }

    public String toString() {
        return "Robot ID = " + ID +", robot is at " + x + ", " + y;
    }

    public boolean isHere(int ix, int iy) {
            if (ix == x && iy == y) {
                return true;
            }
            else 
            {
                return false;
            }
    }


    public static void main(String[] args) {

        RobotArena rrr1;
        Random addro = new Random();
        rrr1 = new RobotArena(20, 5, addro);
        rrr1.addRobot();
        }

}

Even though I never call the toString function, which generates a string for every robot's ID and position in the Arena,

It is called indirectly . You call System.out.println(rrr[c]); and the method is implemented such that it prints argument.toString() of the given argument.

Let's take a look at a recent implemention ( 8u40-b25 ). System.out points to an object of type PrintStream , this is how its println method is implemented:

 public void println(Object x) {
     String s = String.valueOf(x);
     synchronized (this) {
         print(s);
         newLine();
     }
 }

It calls String.valueOf(x) ( implementation ):

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

Which calls obj.toString() on the argument.

这是因为在Java中-如果您打印对象-toString方法将隐式调用

if you do :

System.out.println( rrr[c]);

then the println method is calling the toString for you internally... that is the real explanation of that beh.

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