一个文件Java中有两个公共类 [英] Two public classes in one file java

查看:64
本文介绍了一个文件Java中有两个公共类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这可能是java中的小问题.我们不能在一个文件中定义两个公共类.但是,在《 SCJP学习指南》一书的示例中,提到了以下示例:

Ok, this might be kiddies question in java. We can't define two public classes in one file. But, in one of the examples from the book SCJP study guide, this example was mentioned:

public abstract class A{
    public abstract void show(String data);
}

public class B extends A{
    public void show(String data){
        System.out.println("The string data is "+data);
    }
    public static void main(String [] args){
        B b = new B();
        b.show("Some sample string data");
    }
}

当我将其复制粘贴到netbeans中时,立即引发编译错误,该公共类A应该在单独的文件中提及.SCJP styudy指南中的示例真的错误吗?同样在一些模拟测试中,我发现很多问题都具有这种模式,但是在所有选项中都没有提到编译器错误.在这里担心

When I copy pasted this into netbeans immediately compile error was thrown, that public class A should me mentioned in separate file. Is that example from SCJP styudy guide really wrong? Also in some of the mock test I found many questions having such pattern but in none of the options was a compiler error was mentioned. Getting worried here

推荐答案

是的,一个文件中不允许2个顶级公共类

yes, 2 top level public classes are not allowed in one file

这篇关于一个文件Java中有两个公共类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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