在Java中不使用类型(类名)创建新实例

问题描述 投票:-2回答:1

我找到了此代码。现在我很困惑,origin = new Point(0, 0);是什么意思。如果是实例化/对象创建,则其类型(类名)在其前面。如果是分配,为什么要使用new

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point origin;

    //Four constructors
    public Rectangle() {
        origin = new Point(0, 0);
    }

    public Rectangle(Point p) {
        origin = p;
    }

    public Rectangle(int w, int h) {
        this(new Point(0, 0), w, h);
    }

    public Rectangle(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;
    }

    //A method for moving the rectangle
    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
    }

    //A method for computing the area of the rectangle
    public int area() {
        return width * height;
    }
}
java instance new-operator
1个回答
0
投票

Point是变量的类型。它基本上存储两个值(x和y)。这就是简单的解释。

origin =新Point(0,0)设置变量原点的值

© www.soinside.com 2019 - 2024. All rights reserved.