3

关于静态和动态之间的区别,我仍然有些困惑。据我所知,动态使用对象,而静态使用类型,动态是在运行时解决的,而静态是在编译时解决的。所以 this.lastName.compareTo(s1.lastName) 不应该使用动态绑定吗?

key.compareTo(list[position-1]) 使用动态绑定

public static void insertionSort (Comparable[] list)
{
    for (int index = 1; index < list.length; index++)
    {
        Comparable key = list[index];
        int position = index;
        while (position > 0 && key.compareTo(list[position-1]) < 0) // using dynamic binding
        {
            list[position] = list[position-1];
            position--;
        }
            list[position] = key;
    }
}

为什么 (this.lastName.compareTo(s1.lastName)) 使用静态绑定?

private String firstName;
private String lastName;
private int totalSales;

@Override
public int compareTo(Object o) {
    SalePerson s1 = (SalePerson)o;

    if (this.totalSales > s1.getTotalSales())
    {
        return 1;
    }

    else if (this.totalSales < s1.getTotalSales())
    {
        return -1;
    }

    else //if they are equal
    {
        return (this.lastName.compareTo(s1.lastName)); //why is this static binding??

    }
}
4

2 回答 2

1

在这段代码中

public static void insertionSort (Comparable[] list)
{
    for (int index = 1; index < list.length; index++)
    {
        Comparable key = list[index];
        int position = index;
        while (position > 0 && key.compareTo(list[position-1]) < 0) 
        {
            list[position] = list[position-1];
            position--;
        }
            list[position] = key;
    }
}

key可以是实现Comparable接口的任何东西,因此在编译时编译器不知道确切的类型,因此在运行时通过使用key所指的对象来解析类型。

但在这段代码中,

@Override
public int compareTo(Object o) {
    SalePerson s1 = (SalePerson)o;

    if (this.totalSales > s1.getTotalSales())
    {
        return 1;
    }

    else if (this.totalSales < s1.getTotalSales())
    {
        return -1;
    }

    else //if they are equal
    {
        return (this.lastName.compareTo(s1.lastName));

    }
}

编译器知道s1的类型,因此它使用静态绑定

于 2017-03-10T06:34:01.360 回答
1

您的问题不完整,并且不包括所有相关的代码。然而,这是不同绑定之间的基本区别

Java 既有静态绑定又有动态绑定。绑定是指变量绑定到特定数据类型的时间。

静态/早期绑定在编译时完成:私有、最终和静态方法和变量。也适用于重载方法

动态/后期绑定在运行时完成: 可以被覆盖的方法。这就是在运行时启用多态行为的原因。

为了进一步证明这一点,请查看这段代码,看看您是否可以确定何时是早期绑定和后期绑定:

/* What is the output of the following program? */

public class EarlyLateBinding {

public boolean equals(EarlyLateBinding other) {
    System.out.println("Inside of overloaded Test.equals");
    return false;
}

public static void main(String[] args) {

    Object t1 = new EarlyLateBinding(); //1
    Object t2 = new EarlyLateBinding(); //2
    EarlyLateBinding t3 = new EarlyLateBinding(); //3
    Object o1 = new Object();


    Thread.currentThread().getStackTrace();

    int count = 0;
    System.out.println(count++); 
    t1.equals(t2);//n
    System.out.println(count++);
    t1.equals(t3);//n
    System.out.println(count++); 
    t3.equals(o1);
    System.out.println(count++); 
    t3.equals(t3);
    System.out.println(count++);
    t3.equals(t2);
}
}

回答:

  • ++ 在计数之后,因此返回的结果是递增之前的 0。因此从 0 开始并按预期进行。
  • 唯一实际调用 EarlyLateBinding 对象的 equals 方法的场景是语句 3。
  • 这是因为equals方法被重载(注意:与对象类equals相比不同的方法签名)
  • 因此,EarlyLateBinding 类型在编译时绑定到变量 t3。

.

于 2017-03-10T04:59:54.303 回答