为什么不能保护java主类? [英] Why can't java main class be protected?

查看:102
本文介绍了为什么不能保护java主类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建具有Access说明符public或默认值的Main类。

I can create Main class with Access specifier "public" or default.

但为什么我不能使用protected创建。默认情况下允许为什么不受保护。

But why can't I create with protected. As default itself allowed why not protected.

公开:

public class MainClass {
 public static void main(String[] args) {
 }
}

默认值:

class MainClass {
 public static void main(String[] args) {
 }
}

受保护:

protected class MainClass {
 public static void main(String[] args) {
 }
}

显示错误:
类MainClass的非法修饰符;只有公共的,抽象的和final是允许的MainClass.java SCJP / src第1行Java问题

Its showing error: Illegal modifier for the class MainClass; only public, abstract & final are permitted MainClass.java SCJP/src line 1 Java Problem

推荐答案

protected 涉及向成员提供包含类型访问权限的子类。这里没有包含类型,那是什么意思?

protected relates to giving subclasses of the containing type access to a member. There's no containing type here, so what would it mean?

请注意,这与 main 无关这样......它适用于任何顶级课程。它的 对于要保护的嵌套类型有效:

Note that this has nothing to do with main as such... it applies to any top-level class. It is valid for a nested type to be protected though:

public class Foo {
    protected static class Bar{}
}

这允许 Foo 访问 Bar

这篇关于为什么不能保护java主类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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