class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Class1
{ public static void main( String argv[] )
{
Super sup = new Sub();
System.out.print( sup.index + "," ); // 5,
System.out.println("_____ "+ (new Sub()).index); //2
System.out.println("@@@@@@@@@@" + sup.getClass().toString()); //Sub
sup.printVal(); // Sub Sub mysub=new Sub();
System.out.println(mysub.index + "," ); // 2,Sub
mysub.printVal();
}
}
我不知道 你是在哪个FAQ里看到的. 我想谁都有错的时候希望楼主不要引伸
我对程序添加了些东西, 希望对你理解有些帮助
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Class1
{ public static void main( String argv[] )
{
Super sup = new Sub();
System.out.print( sup.index + "," ); // 5,
System.out.println("_____ "+ (new Sub()).index); //2
System.out.println("@@@@@@@@@@" + sup.getClass().toString()); //Sub
sup.printVal(); // Sub Sub mysub=new Sub();
System.out.println(mysub.index + "," ); // 2,Sub
mysub.printVal();
}
}
我不知道 你是在哪个FAQ里看到的. 我想谁都有错的时候希望楼主不要引伸
我对程序添加了些东西, 希望对你理解有些帮助
看看下面的话,也许会有启发
when a variable of an object is accessed using a reference,it is the type of the reference,not the class of the current object denoted by the reference, that determines which variable will actually be accessed.
Static Binding-- Bind at compile time:The addressing of the variable is determined at compile time.
---------------------------
When a method is invoked on an object using a reference,it is the class of the current object denoted by the reference ,not the type of the reference,that determines which method implementation will be executed.
Dynamic Binding-- Bind at run time:The addressing of the method is determined at run time.