Java学习之内部类

示例1:

 1 package com.swust.面向对象;
 2 
 3 class Person1{
 4     private String username="zhangsan";
 5     public Person1(){
 6         System.out.println("Person created......");
 7     }
 8     public String name ="";
 9     class Student{
10         public Student(){
11             System.out.println("Student created......");
12         }
13         public String username="lisi";
14         public void show(){
15             System.out.println(this.getClass().getName());
16             System.out.println(username);
17         }
18     }
19 }
20 
21 class Person2{
22     private static String username = "lisi";
23     static class Student{
24         public void show(){
25             System.out.println(username);
26         }
27     }
28 }
29 
30 class Person3{
31     private static String useranme="wangwu";
32     static class Student{
33         public  static void show(){
34             System.out.println(useranme);
35         }
36     }
37 }
38 
39 public class Example2{
40     public static void main(String[] args) {
41 //       43 //        person.show();
44 //        //内部类为静态类
45 //        Person2.Student person2 = new Person2.Student();
46 //        person2.show();
47 //       由于内部类持有外部类的引用,因而可以访问外部类的成员变量50 //        Person3.Student.show();
51         
53         Person1.Student person = new Person1().new Student();
54         person.show();
55     }
56 }

对于内部类的访问分为如下情况:

(1)普通情况下,访问内部类必须如下格式 : Person1.Student person = new Person1().new Student();

(2)内部类的方法为静态方法,Person3.Student person3 = new Person3.Student();

(3  局部内部类无法访问方法中的普通变量,将该变量设置为final,常量类型,由于方法调用结束的时候所要调用的参数就消失了,而对该参数添加final修饰符修饰后就变成常量,存储位置发生变化,存储在常量池中

示例2:

 1 package com.swust.面向对象;
 2 abstract class Base{
 3     public abstract void show();
 4 }
 5 
 6 
 7 class Student{
 8     private static int age = 4;
 9     public static void show(){
10         new Base(){
11             @Override
12             public void show() {
13                 System.out.println("匿名内部类……"+age);    
14             }    
15         }.show();;
16     }
17 }
18 public class Example4 {
19     public static void main(String[] args) {
20         Student.show();
21     }
22 }

匿名内部类:内部类的简写形式,其实就是一个匿名子类对象,但是有前提,就是该内部类必须实现继承外部类或者实现一个接口

new Base(){
    @Override
    public void show() {
        System.out.println("匿名内部类……"+age);    
    }    
}
            

整条语句相当于创建一个匿名内部类的对象

示例3:

 1 interface People{
 2     void show1();
 3     void show2();
 4 }
 5 
 6 class Example{
 7     public void show(){
 8         new People(){
 9             public void show1(){
10                 System.out.println("show1 execute……");
11             }
12             public void show2(){
13                 System.out.println("show2 execute……");
14             }
15         }.show1();
16         new People(){
17             public void show1(){
18                 System.out.println("show1 execute……");
19             }
20             public void show2(){
21                 System.out.println("show2 execute……");
22             }
23         }.show2();
24     }
25 }
26 
27 public class Example5 {
28     public static void main(String[] args) {
29         Example example = new Example();
30         example.show();
31     }
32 }

匿名内部类的使用场景:当函数参数是接口类型的时候,而且接口中的方法不超过三个,这时候可以利用匿名内部类作为实际参数进行传递

示例4:

 1 package com.swust.面向对象;
 2 
 3 class Dog{
 4     public void show(){
 5         new Object(){
 6             public void show(){
 7                 System.out.println("匿名内部类执行……");
 8             }
 9         }.show();;
10     }
11     
12     public void message(){
13         Object object = new Object(){
14             public void show(){
15                 System.out.println("匿名内部类执行………………");
16             }
17         };
18     }
19 }
20 
21 
22 public class Example6 {
23     public static void main(String[] args) {
24         new Dog().show();
25     }
26
在这里不能执行:object.show()语句,由于上面语句类似于将匿名内部类子类对象进行向上转型,转换为Object对象,而Object对象中没有show方法
原文地址:https://www.cnblogs.com/sunfie/p/4784637.html