我对Java非常陌生,我正在制作这个程序,随机将机器人放入预定义的舞台。
这是我的两个班级的代码:
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();
}
}
即使我从不调用toString函数,它为每个机器人的ID和竞技场中的位置生成一个字符串,
答案 0 :(得分:2)
它间接称为 。您调用directory$ = "/Users/directorypath"
listfile$ = "test.txt"
Read Strings from raw text file... 'directory$'/'listfile$'
last = Get number of strings
listfile2$ = listfile$ - ".txt"
# loop through each file
for a from 1 to last
select Strings 'listfile2$'
textgrid$ = Get string... 'a'
Read from file... 'directory$'/'textgrid$'
object_name$ = selected$("TextGrid")
Read from file... 'directory$'/'object_name$'.wav
# rearrange tiers
select TextGrid 'object_name$'
Duplicate tier: 3, 1, "MAU"
Remove tier: 4
Insert interval tier: 1, "subphone"
n = Get number of intervals: 3
for i to n
@instance: "strikes"
@instance: "raindrops"
@instance: "and"
@instance: "rainbow"
@instance: "into"
@instance: "round"
@instance: "its"
@instance: "its"
endfor
endfor
procedure instance: .target_word$
label$ = Get label of interval: 3, i
if label$ == .target_word$
index = i
i += n
# get the start and end point of the word
startpoint = Get starting point... 3 index
endpoint = Get end point... 3 index
select TextGrid 'object_name$'
plus Sound 'object_name$'
View & Edit
editor TextGrid 'object_name$'
# annotation
Select... startpoint endpoint
Zoom to selection
pause Annotate stops then continue
Close
endeditor
endif
endproc
并实现该方法,以便打印给定参数的System.out.println(rrr[c]);
。
让我们来看看recent implemention( 8u40-b25 )。 argument.toString()
指向System.out
类型的对象,这是PrintStream
方法的实现方式:
println
它会调用 public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
(implementation):
String.valueOf(x)
在论证上调用public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
。
答案 1 :(得分:-1)
这是因为在java中 - 如果你打印一个对象 - 将隐式调用toString方法
答案 2 :(得分:-1)
如果你这样做:
onExpandedRowsChange
然后println方法在内部为你调用toString ...这就是那个beh的真实解释。