使类受保护时出错

  • 本文关键字:出错 受保护 java
  • 更新时间 :
  • 英文 :


当我在类 Zoo 上使用 protected 关键字时,我收到错误"删除无效修饰符"。

protected class Zoo {
   int  x=5;
 String  coolMethod() {
return "Wow baby";
}
 static Test t=new Test();
}
public class Test {
public static void main(String args[]) {
    Test zoo=new Test();
    //System.out.println(zoo.coolMethod()); 
    //System.out.println(x);
    //t.useMyCoolMethod();
    //Testing v=new Testing();
    //System.out.println(v.getValue());
    }

我该如何解决这个问题?

protected修饰符仅在内部类或嵌套类中有效。

顶级(非内部)类不能protected,只能public或包(默认)访问。

只能是公共的。可以保护内部类

最新更新