变量的多态性初始化 [英] Initialization in polymorphism of variables

查看:175
本文介绍了变量的多态性初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下代码:

  class A {
int i = 4;

A(){
print();
}

void print(){
System.out.println(A);
}
}

类B扩展A {
int i = 2; //this line

public static void main(String [] args){
A a = new B();
a.print();
}

void print(){
System.out.println(i);
}
}

这将打印0 2



现在,如果删除标记为this line的行,
代码将打印4 4




  • 我理解,如果没有int i = 2; ,



A a = new B(); ,初始化i为4,调用构造函数,

它控制 print()方法在 B类,最后输出4.



a.print()会调用 print()方法在类B中,因为方法将在运行时绑定,这也将使用在类A,4定义的值。



(当然如果我的推理有任何错误,让我知道)




  • 然而,我不明白是如果有是int i = 2。



为什么是如果你插入代码,第一部分打印0而不是4?为什么它不初始化变量为i = 4,而是分配默认值?

解决方案


  1. 方法覆盖

  2. 实例变量阴影




  3. 我将简单介绍一下代码中发生的事情,看看你是否明白。



    您的代码在概念上看起来像这样(跳过main()):

      
    int i = 0; // default value

    A(){
    A :: i = 4; //最初在初始化语句
    print();
    }

    void print(){
    System.out.println(A);
    }
    }

    类B扩展A {
    int i = 0; //记住这个阴影A :: i

    public B(){
    super();
    B :: i = 2;
    }

    void print(){
    System.out.println(i);
    }
    }

    > main(),你调用 A a = new B(); ,它构造一个 B ,发生这种情况:




    • A :: i B :: i 均为默认值 0

    • super ,这意味着A的构造函数被调用


      • A :: i 设置为4

      • print()被调用。由于后期绑定,它绑定到 B :: print()

      • B :: print ()尝试打印 B :: i ,它仍然为0


    • B :: i 设置为2


    $ b b

    然后,当您在 main()中调用 a.print() code> B :: print(),它正在输出 B :: i (此时为2) p>

    因此,您看到的结果是


    Suppose you have the following code

    class A {
        int i = 4;
    
        A() { 
            print();
        }
    
        void print () {
            System.out.println("A");
        }
    }
    
    class B extends A {
        int i = 2;              //"this line"
    
        public static void main(String[] args){
            A a = new B();
            a.print();
        }
    
        void print () {
            System.out.println(i);
        }
    }
    

    this will print 0 2

    Now, if you remove line labeled "this line" the code will print 4 4

    • I understand that if there was no int i=2; line,

    A a = new B(); will call class A, initializes i as 4, call constructor,
    which gives control over to print() method in class B, and finally prints 4.

    a.print() will call print() method in class B because the methods will bind at runtime, which will also use the value defined at class A, 4.

    (Of course if there is any mistake in my reasoning, let me know)

    • However, what i don't understand is if there is int i=2.

    why is it that if you insert the code, the first part (creating object) will all of sudden print 0 instead of 4? Why does it not initialize the variable as i=4, but instead assigns default value?

    解决方案

    It is a combination of several behaviors in Java.

    1. Method overriding
    2. Instance variable shadowing
    3. order of constructors

    I will simply go through what happened in your code, and see if you understand.

    Your code conceptually looks like this (skipping main()):

    class A {
        int i = 0; // default value
    
        A() { 
            A::i = 4;  // originally in initialization statement
            print();
        }
    
        void print () {
            System.out.println("A");
        }
    }
    
    class B extends A {
        int i = 0;              // Remember this shadows A::i
    
        public B() {
            super();
            B::i = 2;
        }
    
        void print () {
            System.out.println(i);
        }
    }
    

    So, when in your original main(), you called A a = new B();, it is constructing a B, for which this happens:

    • A::i and B::i are all in default value 0
    • super(), which means A's constructor is called
      • A::i is set to 4
      • print() is called. Due to late-binding, it is bound to B::print()
      • B::print() is trying to print out B::i, which which is still 0
    • B::i is set to 2

    Then when you call a.print() in your main(), it is bounded to B::print() which is printing out B::i (which is 2 at this moment).

    Hence the result you see

    这篇关于变量的多态性初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆