简体   繁体   English

是否可以在使用方法的过程中初始化Dimension的新实例,如果是这样,为什么?

[英]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.

相关问题 方法内部初始化的实例变量 - instance variables initialized inside method 为什么数组类型的类变量在声明后不能初始化,但是可以在Java中的方法内部初始化? - Why class variable of type array can't be initialized after declaration, but can be initialized inside a method in Java? 为什么不能对初始化为TreeSet的Set使用.pollFirst()方法? - Why can't I use .pollFirst() method for a Set that was initialized as a TreeSet? 为什么我们可以使用 'this' 作为实例方法参数? - Why can we use 'this' as an instance method parameter? 如何使用PowerMockito在静态方法中模拟新的类实例 - How to use PowerMockito to mock new class instance inside a static method 为什么我们不能在扩展类的静态方法中使用此实例? - Why can't we use this instance in the static method of extended class? 为什么我可以替换一个新片段,而不替换一个初始化片段? - Why can I replace a new fragment but not a initialized fragment? 在方法内部创建一个新实例,但仍保留在方法外部 - create a new instance inside a method, and it remains outside method 为什么我们不能以常规方式在外部类的main方法内创建内部类的实例? - Why we can not make an instance of inner class inside the main method of outer class in regular way? 为什么我们不能在 java 的子 class 的方法之外访问 class 内部的父实例变量 class? - why can't we access parent class instance variables inside the class outside the method in child class in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM