代码之家  ›  专栏  ›  技术社区  ›  bruno conde

受保护/公共内部类

  •  15
  • bruno conde  · 技术社区  · 17 年前

    有人能向我解释一下两者之间的区别吗 protected / public 班级?

    公共 尽可能避免使用内部类(如下面所解释的 article

    但据我所知,使用 受保护的 公共

    看看这个例子:

    public class Foo1 {
     public Foo1() { }
    
     protected class InnerFoo {
      public InnerFoo() {
       super();
      }
     }
    }
    

    public class Foo2 extends Foo1 {
     public Foo2() {
      Foo1.InnerFoo innerFoo = new Foo1.InnerFoo();
     }
    }
    

    ...

    public class Bar {
     public Bar() {
      Foo1 foo1 = new Foo1();
      Foo1.InnerFoo innerFoo1 = foo1.new InnerFoo();
    
      Foo2 foo2 = new Foo2();
      Foo2.InnerFoo innerFoo2 = foo2.new InnerFoo();
     }
    }
    

    InnerFoo 受保护的 公共 .

    受保护的 公共 .

    3 回复  |  直到 16 年前
        1
  •  23
  •   coobird    17 年前

    protected access修饰符将限制来自同一包及其子类中的类以外的类的访问。

    在所示的示例中 public 受保护的

    有关访问修饰符的更多信息 Controlling Access to Members of a Class 第页 The Java Tutorials 可能会感兴趣。

        2
  •  1
  •   wangzhengyi    11 年前

    此外,对于outter类,它只有两个访问修饰符。只有public和package。

        3
  •  1
  •   KANJICODER    7 年前

    java中的一件怪事:

    纯Java: 您不能返回 私有内部类 来自一位公众人物。

    您不能返回 非公共内部阶级 来自一位公众人物。


    可以运行的Java演示:

    public class ReturnInnerClass{
        public static void main(String []args){
            MyClass inst = new MyClass("[PROP_VAL]");
            System.out.println(
    
                inst.get().myProperty()
    
            );;    
        };;
    };;
    
    class MyClass{ 
        //:If JSP: MUST be public
        //:Pure Java: 
        //:     public,protected,no-access-modifier
        //:     Will all work.
        //:Private fails in both pure java & jsp.
        protected class Getters{
            public String 
            myProperty(){ return(my_property); }
        };;
    
        //:JSP EL can only access functions:
        private Getters _get;
        public  Getters  get(){ return _get; }
    
        private String 
        my_property;
    
        public MyClass(String my_property){
            super();
            this.my_property    = my_property;
            _get = new Getters();
        };;
    };;
    
    //:How to run this example:
    //:  1: Put this code in file called: "ReturnInnerClass.java"
    //:  2: Put ReturnInnerClass.java into it's own folder.
    //:     ( Folder name does not matter.)
    //:  3: Open the folder.
    //:  4: Right-Click --> GitBashHere
    //:  5: In command prompt within folder:
    //:     5.1: javac ReturnInnerClass.java
    //:     5.2: java  ReturnInnerClass
    //:     ( javac: java compiler              )
    //:     ( java : runs compiled java program )
    
    //:  EXPECTED OUTPUT:
    //:  [PROP_VAL]
    

    对于JSP ,只将上面的类代码放入文件夹:com/myPackage/MClass 并将“import com.myPackage.mClass”作为源代码的第一行。然后使用以下源代码创建一个新的.jsp页面:

    <%@ taglib uri   ="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page import="com.myPackage.MyClass" %>
    <%
        MyClass inst = new MyClass("[PROP_VALUE]");
        pageContext.setAttribute("my_inst", inst ); 
    %><html lang="en"><body>
        ${ my_inst.get().myProperty() }
    </body></html>
    

    使用的堆栈:

    推荐文章