有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Point类move()与setLocation()的比较

我想知道java awt Point类的move(int,int)函数和setLocation(int,int)函数之间是否有显著差异。它们中有没有一个运行得更快,或者在后台有一些疯狂的不同功能。他们似乎做了完全相同的动作——只有一个名字更短


共 (2) 个答案

  1. # 1 楼答案

    两者都是一样的。以下是“移动”的作用

    public void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
    

    下面是“设置位置”的作用

    public void setLocation(int x, int y) {
        move(x, y);
    }
    

    它只调用move方法move只接受整型参数,而setLocation则接受整型、双精度和点型参数

  2. # 2 楼答案

    当您对API有疑问时,最好先阅读相关文档

    public void move(int x, int y)

    Moves this point to the specified location in the (x,y) coordinate plane. This method is identical with setLocation(int, int).

    http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html#move(int,%20int)

    我想你可以说一个区别是setLocation是重载的,允许它接受一个Point、两个整数或两个double,而move只接受两个整数