zoukankan      html  css  js  c++  java
  • Java学习之内部类

     1 class Outer
     2 {
     3     private static int num=3;
     4     //1、
     5     class Inner//内部类
     6     {
     7         void show()
     8         {
     9             //内部类直接访问外部类的成员
    10             System.out.println("Show Inner run" + num);
    11         }
    12         
    13         static void function() //内部类中定义静态成员,该内部类必须是静态内部类
    14         {
    15             System.out.println("Show Inner run" + num);
    16         }
    17     }
    18     //2、
    19     static class Inner1//静态内部类
    20     {
    21         void show()
    22         {
    23             //内部类直接访问外部类的成员
    24             System.out.println("Show Inner run" + num);
    25         }
    26     }
    27     
    28     //3、
    29     static class Inner2//静态内部类
    30     {
    31         static void show() //静态成员
    32         {
    33             //内部类直接访问外部类的成员
    34             System.out.println("Show Inner run" + num);
    35         }
    36     }
    37     
    38     public void method()
    39     {
    40         //外部类访问内部类
    41         Inner in=new Inner();
    42         in.show();
    43     }
    44 }
    45 
    46 class InnerClassDemo
    47 {
    48     public static void main(String[] args)
    49     {
    50         //1、
    51         Outer.Inner in=new Outer().new Inner();
    52         in.show();
    53         //2、如果内部类时静态的,相当于一个外部类
    54         Outer.Inner1 in1=new Outer.Inner1();
    55         in.show();
    56         //3、如果内部类时静态的,成员是静态的
    57         Outer.Inner2.show();
    58     }
    59 }

    内部类实例:

     1 class Outer
     2 {
     3     int num=3;
     4     class Inner//内部类
     5     {
     6         int num=4;
     7         void show()
     8         {
     9             int num=5;
    10             System.out.println(num);            //5
    11             System.out.println(this.num);        //4
    12             System.out.println(Outer.this.num);    //3
    13         }
    14     }
    15     
    16     public void method()
    17     {
    18         //外部类访问内部类
    19         new Inner().show();
    20     }
    21 }
    22 
    23 class InnerClassDemo
    24 {
    25     public static void main(String[] args)
    26     {
    27         new Outer().method();
    28     }
    29 }

    内部类可以存放再局部位置上

     1 class Outer
     2 {
     3     int num=3;
     4     
     5     public void method()
     6     {
     9         //内部类访问局部变量,局部变量必须被final修饰
    10         final int x = 9;
    7 class Inner//内部类 8 {11 void show() 12 { 13 System.out.println(x); 14 } 15 } 16 17 Inner in = new Inner(); 18 in.show(); 19 } 20 } 21 22 class InnerClassDemo 23 { 24 public static void main(String[] args) 25 { 26 new Outer().method(); 27 } 28 }
  • 相关阅读:
    【Elasticsearch 技术分享】—— ES 常用名词及结构
    【Elasticsearch 技术分享】—— Elasticsearch ?倒排索引?这都是什么?
    除了读写锁,JUC 下面还有个 StampedLock!还不过来了解一下么?
    小伙伴想写个 IDEA 插件么?这些 API 了解一下!
    部署Microsoft.ReportViewe
    关于TFS强制undo他人check out
    几段查看数据库表占用硬盘空间的tsql
    How to perform validation on sumbit only
    TFS 2012 Disable Multiple Check-out
    在Chrome Console中加载jQuery
  • 原文地址:https://www.cnblogs.com/WarBlog/p/12060817.html
Copyright © 2011-2022 走看看