zoukankan      html  css  js  c++  java
  • Java基础-学习笔记(十)——内部类(嵌套类)

    1、内部类的含义

    在一个类的内部再定义一个类,就叫做嵌套类。被嵌套的类(内部类)可以直接访问嵌套它的类(外部类)的成员函数和属性,哪怕是私有的。但是反过来就不行,外部的类不能直接访问内部类的成员。

    也就是说:

    1)内部类可以直接访问外部类的成员

    2)外部类不可以直接访问内部类的成员,需要先创建内部类的对象,又该对象来调用内部类的成员和方法。

    内部类的定义和普通类没什么区别,不过和外部类不一样的是,它可以被protected和private进行修饰

     1 class Outer
     2 {
     3     int out_i=33;
     4     void test()
     5     {
     6         Inner in = new Inner();
     7         in.display();
     8     }
     9     class Inner
    10     {
    11         void display()
    12         {
    13             System.out.println("the out_i is "+out_i);
    14         }
    15     }
    16 }
    17 class TestOuter
    18 {
    19     public static void main(String [] args)
    20     {
    21         Outer obj=new Outer();
    22         obj.test();
    23     }
    24 }
    25 /*
    26 F:java_example>java TestOuter
    27 the out_i is 33*/
    View Code

    首先调用这个display()函数打印出out_i时,它首先会在display()函数中找是否有该变量,没有就到Inner类中去找,最后再到Outer类中找该变量

    2、内部类的用途

    内部类使程序代码更为紧凑,更具模块化。

    当A类中的程序代码要用到B类的实例对象,而B类中的程序代码又要访问A类中的成员.将B类作为A的内部类,代码要容易编写很多。

    如果我们把内部类Inner,直接拿到外面来,还是要实现这个功能,通过在Outer类中生成Inner对象来调用其display方法,输出outer类的成员变量out_i。代码如下所示:

    class Outer
    {
        int out_i=33;
        void test()
        {
            Inner in = new Inner(this);
            in.display();
        }
        
    }
    class Inner
        {
            
            Outer outer;
            public Inner(Outer outer)
            {
                this.outer=outer;
            }
            void display()
            {
                System.out.println("the out_i is "+outer.out_i);
            }
        }
    class TestOuter
    {
        public static void main(String [] args)
        {
            Outer obj=new Outer();
            obj.test();
        }
    }
    /*
    F:java_example>java TestOuter
    the out_i is 33*/
    View Code

    3、外部程序引用内部类

     1 class Outer
     2 {
     3     int out_i=33;
     4     void test()
     5     {
     6         Inner in = new Inner();
     7         in.display();
     8     }
     9 public class Inner
    10     {
    11         int inner_i=3;
    12         void display()
    13         {
    14             System.out.println("the out_i is "+out_i);
    15         }
    16     }
    17 }
    18 class TestOuter
    19 {
    20     public static void main(String [] args)
    21     {
    22         Outer obj=new Outer();
    23         //obj.test();
    24         Outer.Inner in = obj.new Inner();
    25         System.out.println("the inner_i_i is "+in.inner_i);
    26     }
    27 }
    28 /*
    29 F:java_example>java TestOuter
    30 the inner_i_i is 3*/
    View Code

    内部类声明为public,在外部可以创建其外部类Outer的实例对象,再通过外部类Outer的实例对象创建Inner类的实例对象,最后通过Inner类的实例对象来调用其成员。

    4、内部类用static修饰

    内部类用static修饰后,就相当于是外部类了,不再是内部类

    5、内部类并非只能在类中定义,也可以在几个程序块的范围之类定义内部类。例如,在方法中,甚至在for循环体内部都可以嵌套

    在方法中定义的内部类只能访问方法中的final类型的局部变量,因为局部变量的作用域只在这个方法内,我们在外部程序调用时,这个局部变量内存已经被收回,所以需要用final修饰,final定义的局部变量相当于是一个常量,它的生命周期超出方法运行的生命周期

     1 class Outer
     2 {
     3     int out_i=33;
     4     void test()
     5     {
     6         final int x=2;
     7         class Inner
     8         {
     9             int inner_i=3;
    10             void display()
    11             {
    12                 System.out.println("the out_i is "+out_i+" and x is "+x);
    13             }
    14         }
    15         Inner in = new Inner();
    16         in.display();
    17     }
    18 
    19 }
    20 class TestOuter
    21 {
    22     public static void main(String [] args)
    23     {
    24         Outer obj=new Outer();
    25         obj.test();
    26     }
    27 }
    28 /*
    29 F:java_example>java TestOuter
    30 the out_i is 33 and x is 2*/
    View Code
  • 相关阅读:
    函数防抖与函数节流 封装好的debounce和throttle函数
    机顶盒
    getchar() putchar()
    【整】char、varchar、nchar、nvarchar的区别
    主机名
    主机
    java中的匿名内部类总结
    智能路由器又多一个玩家——乐视TV
    乐视开始折腾路由器,小米与极路由还会好过吗?
    带你认识什么是路由器
  • 原文地址:https://www.cnblogs.com/tiantianxiangshang33/p/4943941.html
Copyright © 2011-2022 走看看