[英]Can a new instance of Dimension be initialized inside the use of a method and if so why?
I'm reading through some java tutorial code on 2d game design and came across this statement . 我正在阅读关于2D游戏设计的一些java教程代码并且遇到了这个声明。
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT)) setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT))
I don't understand how a new instance of Dimension can be initialized inside the use of the method setPrefferedSize(). 我不明白如何在使用setPrefferedSize()方法的过程中初始化一个新的Dimension实例。 I'm not even sure if I'm using all the proper vocabulary but if someone understands what I'm asking help would be appreciated =).
我甚至不确定我是否使用了所有合适的词汇,但如果有人理解我所要求的帮助将会受到赞赏=)。 Below is the full code
以下是完整的代码
public class Board extends JPanel { 公共类委员会延伸JPanel {
private final int B_WIDTH = 350;
private final int B_HEIGHT = 350;
private final int INITIAL_X = -40;
private final int INITIAL_Y = -40;
private final int INITIAL_DELAY = 100;
private final int PERIOD_INTERVAL = 25;
private Image star;
private Timer timer;
private int x, y;
public Board() {
initBoard();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("star.png");
star = ii.getImage();
}
private void initBoard() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
setDoubleBuffered(true);
loadImage();
x = INITIAL_X;
y = INITIAL_Y;
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(),
INITIAL_DELAY, PERIOD_INTERVAL);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawStar(g);
}
private void drawStar(Graphics g) {
g.drawImage(star, x, y, this);
Toolkit.getDefaultToolkit().sync();
}
private class ScheduleTask extends TimerTask {
@Override
public void run() {
x += 1;
y += 1;
if (y > B_HEIGHT) {
y = INITIAL_Y;
x = INITIAL_X;
}
repaint();
}
}
} }
There is nothing unusual. 没有什么不寻常的。 You could have written
你可以写
Dimension d=new Dimension(B_WIDTH, B_HEIGHT); 尺寸d =新尺寸(B_WIDTH,B_HEIGHT); setPreferredSize(d);
了setPreferredSize(d);
It's the same thing. 这是同一件事。 But since you won't use it anywhere else, it's the fastest way of writing it.
但由于你不会在其他任何地方使用它,这是编写它的最快方式。
When you call a method, as in : 调用方法时,如:
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT))
first the arguments are evaluated. 首先评估参数。 In this case there is a single argument -
new Dimension(B_WIDTH, B_HEIGHT)
- an its evaluation creates a Dimension instance. 在这种情况下,有一个参数 -
new Dimension(B_WIDTH, B_HEIGHT)
- 其评估创建一个维度实例。
Then that instance is then passed to the setPreferredSize
method. 然后将该实例传递给
setPreferredSize
方法。
The result of constructing an object is the object itself. 构造对象的结果是对象本身。
Dimension prefSize = new Dimension(B_WIDTH, B_HEIGHT);
An object can be constructed without assigning it to a variable, and the result is still a new object. 可以构造一个对象而不将其赋值给变量,结果仍然是一个新对象。
new Dimension(B_WIDTH, B_HEIGHT);
Though the above by itself is not a valid statement, because we're required to do something with the object. 虽然上面本身并不是一个有效的陈述,因为我们需要对该对象做一些事情。 Passing it to a method is a valid something.
将它传递给方法是有效的。
The author could have written it like 作者本可以写出来的
Dimension prefSize = new Dimension(B_WIDTH, B_HEIGHT);
setPreferredSize(prefSize);
but there was no point in the intermediate assignment to a variable because the Dimension object was being created solely for the purpose of setting that one component's preferred size. 但是对变量的中间赋值没有意义,因为Dimension对象的创建仅仅是为了设置一个组件的首选大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.