Java 错误:预期的类、接口或枚举 [英] Java error: class, interface, or enum expected

查看:36
本文介绍了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?

推荐答案

例如:

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天全站免登陆