Java错误:需要类,接口或枚举 [英] Java error: class, interface, or enum expected

查看:182
本文介绍了Java错误:需要类,接口或枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道这段代码的输出。但它不工作。也许代码是错误的。
我还在学习如何使用Java,我试图修复这个小时,但仍然没有运气。

I need to know the output of this code. But it's not working. Maybe the code is wrong. I'm still learning how to use Java, and I tried fixing this for hours but still no luck.

以下是代码:

public class A 
{ 
    public A() 
    {
        System.out.println ("A");
    }
}
public class B extends A 
{
    public B() 
    {
        System.out.println ("B");
    }
}
public class C extends B 
{ 
    public C() 
    {
        System.out.println ("C");
    }
}

public static void main(String args[]) {

    A a = new A();  
    B b = new B();  
    C c = new C();  
}

任何人都可以告诉我代码中有什么错误或缺失?谢谢。

Can anyone tell me what is wrong or missing in the code? Thank you.

推荐答案

例如:

public class Example {

    public static void main(String...args) {
        new C();
    }

    public static class A {
        public A() {
            System.out.println("A");
        }
    }
    public static class B extends A {
        public B() {
            System.out.println("B");
        }
    }
    public static class C extends B {
        public C() {
            System.out.println("C");
        }
    }
}

打印你会期望的。它实际上会打印:

Also note that this might not print what you would expect. It would actually print:

A
B
C

为什么?构造函数总是链接到超类。

Why? Constructors are always chained to the super class.

这篇关于Java错误:需要类,接口或枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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