我的输出是否正确?

时间:2014-03-13 03:23:15

标签: java inheritance

每个构造函数的以下输出是否正确?

class Blue {

    public Blue() {
        System.out.println("Bird");
    }

    public Blue(int i) {
    }
}

class Red extends Blue {

    public Red() {
        super(123);
        System.out.println("Dog");
    }
}

class White extends Red {

    public White() {
        System.out.println("Cat");
    }
}

class Orange extends White {

    public Orange() {
        super();
        System.out.println("Cow");
    }
}

到目前为止,new Blue()将打印"牛,猫,狗",new Red()将打印"狗",new White()将打印& #34;狗"并且new Orange()将打印#34; Bird"。这看起来不错吗?

1 个答案:

答案 0 :(得分:1)

基本解决方案是从那里创建一个新类,可能称为Main,创建public static void main(String args[])方法并简单地创建每个对象的新实例......

public class Main {

    public static void main(String[] args) {
        new    Blue();
        System.out.println("...");
        new Red();
        System.out.println("...");
        new White();
        System.out.println("...");
        new Orange();
    }

}

如果您使用的是IDE,那么您就可以运行此类并查看输出...

相关问题