如题
一个一个重载的方法好像太麻烦,有没有简单点的办法啊?

解决方案 »

  1.   

    是这个吗?package c05;
    public class E06_AccessControl {
      public int a;
      private int b;
      protected int c;
      int d; // "Friendly"
      public void f1() {}
      private void f2() {}
      protected void f3() {}
      void f4() {} // "Friendly"
      public static void main(String args[]) {
        E06_AccessControl test =
          new E06_AccessControl();
        test.a = 1;
        test.b = 2;
        test.c = 3;
        test.d = 4;
        test.f1();
        test.f2();
        test.f3();
        test.f4();
      }
    }  ///:~
    You can see that main( ) has access to everything because it’s a member. If you create a separate class within the same package, that class cannot access the private members:package c05;
    public class E06_Other {
      public E06_Other() {
        E06_AccessControl test =
          new E06_AccessControl();
        test.a = 1;
        //! test.b = 2; // Can't access:  private
        test.c = 3;
        test.d = 4;
        test.f1();
        test.f3();
        test.f4();
      }
    } ///:~
    If you create a class in a separate package (which you can do either by explicitly giving a package statement, or by simply putting it in a different directory) then it is unable to access anything but public members:
    //: c05:other2:E06_Other2.java
    // A separate class in the same package cannot
    // access private elements:
    package c05.other2;
    public class E06_Other2 {
      public E06_Other2() {
        c05.E06_AccessControl test =
          new c05.E06_AccessControl();
        test.a = 1;
        test.f1();
      }
    } ///:~