方法重载和重写原理

Java的编译不包含传统编译过程的连接阶段,连接是在class加载到JVM中才进行的。Java代码在编译成class文件后,里面所有的方法调用都替换成了符号引用,这些符号引用需要在解析阶段解析成实际的内存地址,也就是直接引用。
JVM提供了5条调用方法的字节码指令:

  • invokestatic: 调用静态方法
  • invokespecial: 调用构造器方法、私有方法
  • invokevirtual : 调用所有的虚方法
  • invokeinterface: 调用接口方法
  • invokedynamic :动态方法调用

重载

看一个简单的代码示例:

 public class OverloadTest {

    void test(int a) {

    }

    void test(String b) {
    }

    public static void main(String[] args) {
        OverloadTest o = new OverloadTest();
        o.test(1);
        o.test("s");
    }
}

反编译:

$ javap -c OverloadTest
警告: 二进制文件OverloadTest包含test.method.OverloadTest
Compiled from "OverloadTest.java"
public class test.method.OverloadTest {
  public test.method.OverloadTest();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  void test(int);
    Code:
       0: return

  void test(java.lang.String);
    Code:
       0: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #1                  // class test/method/OverloadTest
       3: dup
       4: invokespecial #23                 // Method "<init>":()V
       7: astore_1
       8: aload_1
       9: iconst_1
      10: invokevirtual #24                 // Method test:(I)V
      13: aload_1
      14: ldc           #26                 // String s
      16: invokevirtual #28                 // Method test:(Ljava/lang/String;)V
      19: return
}

可以发现,在编译完成时,重载方法的两次调用分别是:

invokevirtual #24                 // Method test:(I)V
invokevirtual #28                 // Method test:(Ljava/lang/String;)V

也就是方法版本(签名)已经确定 .
再一个涉及继承的重载示例:

public class OverloadTest2 {

    static class Super {

    }

    static class Sub extends Super {

    }

    static void test(Super s) {
        System.out.println("super");
    }

    static void test(Sub s) {
        System.out.println("sub");
    }

    public static void main(String[] args) {
        Super s = new Super();
        test(s);
        s = new Sub();
        test(s);
        Sub sub = new Sub();
        test(sub);
    }
}

反编译:

$ javap -c OverloadTest2
警告: 二进制文件OverloadTest2包含test.method.OverloadTest2
Compiled from "OverloadTest2.java"
public class test.method.OverloadTest2 {
  public test.method.OverloadTest2();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  static void test(test.method.OverloadTest2$Super);
    Code:
       0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #22                 // String super
       5: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

  static void test(test.method.OverloadTest2$Sub);
    Code:
       0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #33                 // String sub
       5: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #38                 // class test/method/OverloadTest2$Super
       3: dup
       4: invokespecial #40                 // Method test/method/OverloadTest2$Super."<init>":()V
       7: astore_1
       8: aload_1
       9: invokestatic  #41                 // Method test:(Ltest/method/OverloadTest2$Super;)V
      12: new           #43                 // class test/method/OverloadTest2$Sub
      15: dup
      16: invokespecial #45                 // Method test/method/OverloadTest2$Sub."<init>":()V
      19: astore_1
      20: aload_1
      21: invokestatic  #41                 // Method test:(Ltest/method/OverloadTest2$Super;)V
      24: new           #43                 // class test/method/OverloadTest2$Sub
      27: dup
      28: invokespecial #45                 // Method test/method/OverloadTest2$Sub."<init>":()V
      31: astore_2
      32: aload_2
      33: invokestatic  #46                 // Method test:(Ltest/method/OverloadTest2$Sub;)V
      36: return
}


对应关系:

        Super s = new Super();
        test(s);                //9: invokestatic  #41                 // Method test:(Ltest/method/OverloadTest2$Super;)V
        s = new Sub();
        test(s);                //21: invokestatic  #41                 // Method test:(Ltest/method/OverloadTest2$Super;)V
        Sub sub = new Sub();
        test(sub);              //33: invokestatic  #46                 // Method test:(Ltest/method/OverloadTest2$Sub;)V

和上面的一样,在编译期确定了方法的版本。可以看出,Java在实现重载时,是使用静态选择的,为什么这么说呢,因为在编译期,要调用哪个重载方法已经确定下来了(看字节码就知道已经定下来了,为什么要在这个时候定下来,设计如此),另外在选择方法时,是依据对象的声明类型来绑定方法的,实际上为了在编译期确定方法,也只能通过对象的声明类型即静态类型,因为动态类型编译器也不知道啊。

重写

public class OverrideTest {
    static class Super {
        void test() {
            System.out.println("super method");
        }
    }

    static class Sub extends Super {
        void test() {
            System.out.println("sub method");

        }
    }

    public static void main(String[] args) {
        Super s = new Super();
        s.test(); //super method
        s = new Sub();
        s.test(); // sub method
    }
}

反编译:

$ javap -c OverrideTest
警告: 二进制文件OverrideTest包含test.method.OverrideTest
Compiled from "OverrideTest.java"
public class test.method.OverrideTest {
  public test.method.OverrideTest();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #16                 // class test/method/OverrideTest$Super
       3: dup
       4: invokespecial #18                 // Method test/method/OverrideTest$Super."<init>":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #19                 // Method test/method/OverrideTest$Super.test:()V
      12: new           #22                 // class test/method/OverrideTest$Sub
      15: dup
      16: invokespecial #24                 // Method test/method/OverrideTest$Sub."<init>":()V
      19: astore_1
      20: aload_1
      21: invokevirtual #19                 // Method test/method/OverrideTest$Super.test:()V
      24: return
}

源码和字节码指令对应:

    Super s = new Super();
    s.test();     //9: invokevirtual #19                 // Method test/method/OverrideTest$Super.test:()V
    s = new Sub();//21: invokevirtual #19                 // Method test/method/OverrideTest$Super.test:()V
    s.test();

和重载一样,对应方法的签名在编译期也是确定的,当然是根据静态类型来确定的。那么最后怎么就进入了子类重写的方法呢?
这就要看一下invokevirtual指令的逻辑了:

If the resolved method is not signature polymorphic (§2.9), then the invokevirtual instruction proceeds as follows.
Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure:
• If C contains a declaration for an instance method m that overrides (§5.4.5) the resolved method, then m is the method to be invoked, and the lookup procedure terminates.
• Otherwise, if C has a superclass, this same lookup procedure is performed recursively using the direct superclass of C; the method to be invoked is the result of the recursive invocation of
this lookup procedure.
• Otherwise, an AbstractMethodError is raised.
(未涉及同步和native方法的lookup逻辑)

也就是先在本类中找此签名的方法,没找到就去父类递归寻找,如果一直找不到,就抛异常。

所以方法签名在编译期确定,具体调用子类还是父类的方法,得看接受者的实际类型,得在运行期确定。

重写的注意点

  • 子类方法的访问权限不得小于父类
  • 子类抛出的异常不能比父类抽象
    这两个限制是为了兼容里式替换原则(Liskov Substitution Principle LSP)。

里式替换原则
任何基类可以出现的地方,子类一定可以出现。

  • 子类不能重写父类私有和静态方法 -> 可以在子类写,但是不是重写的效果,因为是私有的(实例私有和类私有,方法的确认过程上面已经说的很清楚了)
class Super {
    static void a() {
    }

    final private void run() {
        System.out.println("super");
    }

    public void b() {

    }

    public void c() throws IOException {

    }
}

class Sub extends Super {
    // 不是重写,是Class持有的
    static void a() {
    }

    // 不是重写,是自己私有的
    final private void run() {
        System.out.println("sub");
    }
    // void b(){ 错误,不能比父类的访问权限低
    //
    // }
    // 不能比父类方法抛出的异常范围更大
    // public void c()throws Exception{
    //
    // }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,715评论 0 9
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,631评论 18 399
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 4,158评论 2 7
  • (楔子) …… 他转身刚想走,忽然听到从第五格子里面猛地发出一声脆响!邹超一个激灵——是一个人的咳嗽声?! 可是,...
    珏诗阅读 400评论 0 0
  • 从小到大,一直慌于表达这件事。 在文字的天地中游刃有余,在语言的世界里却总难恰如其分。 常常觉得如鲠在喉,更怕说错...
    优雅行吟阅读 1,109评论 0 1