昨天我接受了两个小时的技术电话采访(我通过了,哇喔!),但我完全消除了关于Java中动态绑定的以下问题.这让我感到非常困惑,因为几年前,当我还是TA时,我曾经向大学生传授这个概念,所以我给他们错误信息的前景有点令人不安......
这是我给出的问题:
/* What is the output of the following program? */
public class Test {
public boolean equals( Test other ) {
System.out.println( "Inside of Test.equals" );
return false;
}
public static void main( String [] args ) {
Object t1 = new Test();
Object t2 = new Test();
Test t3 = new Test();
Object o1 = new Object();
int count = 0;
System.out.println( count++ );// prints 0
t1.equals( t2 ) ;
System.out.println( count++ );// prints 1
t1.equals( t3 …Run Code Online (Sandbox Code Playgroud) 我对动态绑定和静态绑定感到困惑.我已经读过在编译时确定对象的类型称为静态绑定,在运行时确定它称为动态绑定.
以下代码中会发生什么:
静态绑定还是动态绑定?
这显示了什么样的多态性?
class Animal
{
void eat()
{
System.out.println("Animal is eating");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog is eating");
}
}
public static void main(String args[])
{
Animal a=new Animal();
a.eat();
}
Run Code Online (Sandbox Code Playgroud) 假设我要覆盖Object的equals()方法:
public boolean equals(Object o){
//something
}
public boolean equals(SomeClass s){
//something else
}
Run Code Online (Sandbox Code Playgroud)
SomeClass显然也是一个Object,如果我使用带有SomeClass实例的equals作为参数,那么将调用哪个方法?