• 编译期
    • 静态成员
    • 私有变量
    • 成员变量
  • 运行期
    • 非静态方法
  • package day1401;
    
    public class Test1 {
        public static void main(String[] args) {
    
            B b = new B();
            A a = new B();
            
            // 运行期, 并绑定到子类方法
            b.p();
            a.p();
            
            // 编译期查找
            // 编译期根据变量 a 的类型定义, 
            // 在这个类中查找 f()方法并绑定
            
            B.f();
            A.f();
            
            /*
             * 编译期绑定
             * 
             */
            System.out.println(b.v1);
            System.out.println(a.v1);
        }
    }
    
    
    class A{
        
        int v1 = 2;
        static void f() {
            System.out.println("A.f()");
        }
        
        void p() {
            System.out.println("A.p()");
        }
        
    }
    
    
    class B extends A {
        int v1 = 1;
        static void f() {
            System.out.println("B.f()");
        }
        
        void p() {
            System.out.println("B.p()");
        }
            
    }

     

相关文章:

  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2019-08-16
  • 2021-11-30
  • 2021-08-03
  • 2022-01-31
猜你喜欢
  • 2021-12-15
  • 2021-06-28
  • 2022-12-23
  • 2021-11-29
  • 2021-11-09
  • 2021-07-22
相关资源
相似解决方案