java code:
package example; class m1 { public int getInt() { return 0; } } class m2 extends m1 { public int getInt() { return 1; } } public class Override { public static void main(String[] args) { m1 m = new m2(); m.getInt(); System.out.println(m.getInt()); } } //输出的是1,说明是调用的是m2的getInt()方法
c# code:
namespace test1 { class Override { public int getInt() { return 0; } } class InheritOverride : Override { public int getInt() { return 1; } } class G0 { static void Main() { Override i = new InheritOverride(); int j = i.getInt(); Console.Write(j);//输出的是0,说明调用的是父类的getInt()方法 Console.ReadKey(); } } }
区别是早期加载和晚期加载
晚期加载
m1 m = new m2();
调用的是m2的方法
早期加载
m1 m = new m2();