zoukankan      html  css  js  c++  java
  • Quiz: what’s the result of below code?

    public class Quiz {
    public static void main(String[] args)
    {
    A aa = new A();
    B bb = new B();
    aa.f();
    bb.f();
    }
    }
    class A
    {
    public void f()
    {
    System.out.printf(“AAAA\n”);
    }
    }
    
    class B extends A
    {
    public void f(int i)
    {
    System.out.printf(“BBBB\n”);
    }
    }

    Look very simple. You may think result is:
    AAAA
    BBBB
    Then, it’s wrong.

    B extends A, and has a f() method as A. You may think it’s “overriding”, but be careful, the f() method in B has a parameter “int i”. So in fact it’s not an “overriding”, but a “overloading” of B’s method f() — Override need same parameter(s).
    In the main, we called aa.f() and bb.f(). For aa.f(), easy, just output “AAAA”.
    But for bb.f(), this call doesn’t have a parameter. so in fact it’s trying to call the f() which is extended from A.
    So the result is:
    AAAA
    AAAA

  • 相关阅读:
    梅森素数
    高精度乘法
    快速幂取模
    高精度加法
    Linux 压缩&解压缩
    Lec_Cloud资源云平台
    IP102,IP102-K_V3.0 输入节点机
    A7互动主机
    音频处理器的使用
    关于测试随笔
  • 原文地址:https://www.cnblogs.com/backpacker/p/2467432.html
Copyright © 2011-2022 走看看