首页 > 试题广场 >

阅读以下程序并写出运行结果:

[问答题]
阅读以下程序并写出运行结果:

public class Reader03 {

    public static void main(String[] args) {

        Operator[] array = {new P1(), new P2(), new P3(), new P4()};

        for (int i = 0; i < array.length; i++) {
            array[i].play();
        }

    }

}

interface Operator {

    public static final int MODE = 1;

    void play();

}

class P1 implements Operator {

    public void play() {  System.out.println("P1 Mode-" + MODE);  }

}

class P2 implements Operator {

    public static final int MODE = 2;

    public void play() {  
        System.out.println("P2 Mode-" + MODE);  
    }

}

class P3 extends P1 {

    public static final int MODE = 3;

}

class P4 extends P2 {

    public void play() {  
        System.out.println("P4 Mode-" + MODE);  
}

}

我在电脑上运行了结果是 P1Mode 1 P2Mode 1 P1Mode 1 P4Mode 1 有没有大神讲解下呀
发表于 2018-01-24 12:23:27 回复(2)

P1 Mode-1

P2 Mode-2

P1 Mode-1

P4 Mode-2
发表于 2017-05-07 15:07:09 回复(0)