zoukankan      html  css  js  c++  java
  • 内部类

    内部类访问规则

    • 内部类可以直接访问外部类中的成员,包括私有。访问格式:外部类名.this
    • 外部类要访问内部类必须创建内部类对象。
    • 内部类在成员位置上,可以被成员修饰符修饰。
     1 public class InnerClassDemo1 {
     2     public static void main(String[] args){
     3         Outer ou =new Outer();
     4         ou.method();// 4  3
     5         Outer.Inner oi =new Outer().new Inner();
     6         oi.function2();
     7         
     8     }
     9 
    10 }
    11 class Outer{
    12     private int x=3;
    13     class Inner{
    14         int x=4;
    15         void function1(){
    16             System.out.println(x);
    17             System.out.println(Outer.this.x);
    18         }
    19         void function2(){
    20             Outer.this.method();
    21         }
    22     }
    23     public void method(){
    24         Inner in =new Inner();
    25         in.function1();
    26     }
    27 }

    静态内部类

    • 内部类定义了静态成员,内部类必须是静态的。
     1 /*
     2 注意:内部类定义了静态成员,内部类必须是静态的。
     3 
     4 */
     5 class InnerClassDemo2 
     6 {
     7     public static void main(String[] args) 
     8     {
     9         new Outer.Inner().function();//创建静态内部类对象
    10     }
    11 }
    12 class Outer
    13 {
    14     private static int x=5;
    15     static class Inner//静态内部类 只能访问外部类的静态成员
    16     {
    17         void function()
    18         {
    19             System.out.println("inner:"+x);
    20         }
    21     }
    22 }

    局部内部类

    • 内部类定义在局部时,可以直接访问外部类中的成员。
    • 访问局部变量必须被final修饰。
     1 /*
     2 内部类定义在局部时:可以直接访问外部类中的成员,因为还持有外类中的引用。
     3 但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量
     4 
     5 */
     6 
     7 class  InnerClassDemo3
     8 {
     9     public static void main(String[] args) 
    10     {
    11         Outer out =new Outer();
    12         out.method(7);
    13     }
    14 }
    15 class Outer
    16 {
    17     int x=3;
    18     public void method(final int a)
    19     {
    20         class Inner
    21         {
    22             void function()
    23             {
    24                 System.out.println(a);
    25             }
    26         }
    27         new Inner().function();
    28     }
    29 }

    匿名内部类

    • 匿名内部类是内部类的简写格式。
    • 匿名内部类的前提:内部类必须继承一个类或者实现接口。
    • 匿名内部类不能创建构造函数。
     1 /*
     2 匿名内部类
     3 1.匿名内部类其实就是内部类的简写格式
     4 2.定义匿名内部列的前提。
     5   内部类必须是继承一个类或者实现接口
     6 3.匿名内部类的格式:new 父类或者接口(){定义子类内容}
     7 */
     8 class InnerClassDemo4 
     9 {
    10     public static void main(String[] args) 
    11     {
    12         Demo d=new Demo()
    13         {
    14             void show()
    15             {
    16                 System.out.println("匿名内部类show方法");
    17             }
    18             void method()
    19             {
    20                 System.out.println("匿名内部类method方法");
    21             }
    22         }.show();
    23         d.method();
    24     }
    25 }
    26 abstract class Demo
    27 {
    28     abstract void show();
    29 }

                                                                                                                                                                                                                                                                                                 ......

  • 相关阅读:
    How to disable daily upgrade and clean on Ubuntu 16.04
    跟李沐学Ai 03 安装【动手学深度学习v2】
    When using 'npm' it requires me to login to github
    Wrong detect of Parsing error: invalid-first-character-of-tag-name in expression.
    18.04: a stop job is running for unattended upgrades shutdown
    NCSC recommend against forcing regular password expiry,
    OWASP Testing for Weak Password Policy--> When must a user change their password
    NIST Special Publication 800-63: Digital Identity Guidelines Frequently Asked Questions
    Mo Tu We Th Fr Sa Su
    工程变更-ECN,ECO,ECR,ECA
  • 原文地址:https://www.cnblogs.com/malinkang/p/2564511.html
Copyright © 2011-2022 走看看