zoukankan      html  css  js  c++  java
  • Java内部类——成员内部类

    成员内部类的意思就是,一个外层类里面包含着一个非static的class,举例如下:

    class OuterClass
    {
            //变量,函数定义。。。
    
            class InnerClass
            {
                             //变量,函数定义。。。
            }
    }    

    关于成员内部类的各种使用情况:

    1.成员内部类不能定义静态成员,原因如下:

      对于java类加载顺序我们知道,首先加载类,执行static变量初始化,接下来执行对象的创建,如果我们要执行代码中的变量i初始化,那么必须先执行加载OuterClass,再加载InnerClass,最后初始化静态变量i,问题就出在加载Innerclass上面,我们可以把InnerClass看成OuterClass的非静态成员,它的初始化必须在外部类对象创建后以后进行,要加载InnerClass必须在实例化OuterClass之后完成 ,java虚拟机要求所有的静态变量必须在对象创建之前完成,这样便产生了矛盾。也就是说,成员内部类是需要在外层类实例化之后,才能加载,其实成员内部类是属于类对象,而不是属于类,所以如果你InnerClass中如果有静态成员,则OuterClass加载的时候,也必须加载了InnerClass,而此时没有OuterClass实例对象,就不会有InnerClass。

    2.当内部类和外部类具有同名成员(变量or函数,这里指的是 非静态的)时:

      如果要获取InnerClass中的变量,直接使用变量名or方法名来调用;如果要调用OuterClass的同名成员,则通过“OuterClass.this.成员”,也就是“外层类.this.成员”。

    3.当外部类有某一成员(变量or函数,静态,非静态都可以),但内部类没有时:

      在InnerClass调用OuterClass的成员,直接通过变量名or函数名调用即可。

    4.当外部成员为静态时(这里不考虑内部类为静态成员的情况,因为这篇文章仅涉及成员内部类的情况,且成员内部类不允许定义静态成员):

      当外部成员为静态时,InnerClass直接通过变量名or函数名调用即可。

    5.外部类,无法直接操作内部类的成员。

      只能通过InnerClass的对象来调用,这里没有考虑InnerClass为静态的情况,(or没有考虑InnerClass中的静态方法,因为成员内部类不能有静态成员)。

    6.外部类的private成员,在内部类中的使用规则和以上五条一致。

    7.如果涉及到继承,也会将这个类继承下来

    class test
    {
        public static void main(String[] args) 
        {
            Human h=new Human();
            Human.China hc=h.new China();
    
            System.out.println(hc.cStr);//将会输出China
        }
    }
    class Person
    {
        public String pStr="Person";
    
        class China
        {
            public String cStr="China";
        }
    }
    class Human extends Person
    {
          //其他属性方法
    }

    实例代码:

    class InnerClassTest 
    {
        public static void main(String[] args) 
        {
            OuterClass oc=new OuterClass();
            OuterClass.InnerClass ic=oc.new InnerClass();//新建一个成员内部类的对象
            OuterClass.InnerClass ic=new OuterClass().new InnerClass();//这样新建一个成员内部类的对象,只是它属于一个新建的OuterClass对象
    
    
            System.out.println(ic.GetSamVar());//调用和内部类同名的变量
            System.out.println();    
            
            System.out.println(ic.GetNotSameVarFromOuter());//调用外部类中没有和内部类同名的变量
            System.out.println();
    
            System.out.println(ic.GetStaticVar());//调用外部类的静态变量
            System.out.println();
    
            System.out.println(ic.CallOuterClassFunction());//调用外部类的非静态方法
            System.out.println();
    
            System.out.println(ic.CallOuterClassStaticFunction());//调用外部类的静态方法
            System.out.println();
    
            System.out.println(ic.CallOuterSameFunctionNameWithInner());//调用外部类中与内部类同名的非静态方法
            System.out.println();
    
            System.out.println(ic.CallOuterClassStaticFunction());//调用外部类中的静态方法
            System.out.println();
        }
    }
    //外部类
    class OuterClass
    {
        int OuterInt=123;
        String OuterString="I am OuterClass's String";
        String sameVar="I am the OuterClass's sameVar";//declare the same variable with the InnerClass.to show how to use the OuterClass and InnerClass var in the InnerClass
        static String staticVar="I am the OuterClass's staticVar";
        //static String staticSameVar="I am the OuterClass's staticSameVar";
    
        public String OuterClassFunction()
        {
            return "I am the OuterClass's function";
        }
    
        public static String OuterClassStaticFunction()
        {
            return "I am the OuterClass's Static function";
        }
      //与内部类同名的方法
        public String SameFunction()
        {
            return "I am the OuterClass's function which has the same name with the InnerClass";
        }
    
    //内部类
    class InnerClass { String sameVar="I am the OuterClass's sameVar"; //static String staticSameVar="I am the OuterClass's staticVar"; //与外部类同名的方法 public String SameFunction() { return "I am the InnerClass's function which has the same name with the OuterClass"; } //与外部类同名的变量 public String GetSamVar()//this function is to distinguish the same name variable with the OuterClass { //return sameVar; //得到内部类的变量 return OuterClass.this.sameVar;//get the OuterClass's sameVar } public String GetNotSameVarFromOuter()//Get the variable from outerclass ,and the variable is not in the innerclass { return OuterString; } public String GetStaticVar()//to show how to use the outerClass's static variable which not in the InnerClass { return staticVar; } public String CallOuterClassFunction()//调用外部类函数 { return OuterClass.this.OuterClassFunction(); } public String CallOuterClassStaticFunction() { return OuterClass.this.OuterClassStaticFunction(); //return OuterClass.OuterClassStaticFunction();//this two kinds of call ,are ok } public String CallOuterSameFunctionNameWithInner() { return OuterClass.this.SameFunction(); } } }

    结果 如图下图 所示:

    各种情况应该都考虑到了,如果有其他情况,赶紧留言,我测试后,再继续更新本文。如果有理解错误的地方,谢谢提出。。。

  • 相关阅读:
    ZOJ 3795 Grouping
    ZOJ 3791 An Easy Game
    ZOJ 3790 Consecutive Blocks
    POJ 1451 T9
    POJ 1141 Brackets Sequence
    POJ 2411 Mondriaan's Dream
    POJ 2513 Colored Sticks
    Eclipse 快捷键大全
    C# lock关键字(多线程)
    C# 内部类
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/4681546.html
Copyright © 2011-2022 走看看