JavaSE:11、内部类

学习 资源1

学习资源 2

1、成员内部类

java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Person person=new Person();//Person构造函数
       Person.Woman woman=person.new Woman();//woman构造函数
        
    }
}
java 复制代码
package com.test;

public class Person {

     public Person(){
         System.out.println("Person构造函数");
     }
      public class Woman{
       public  Woman()
       {
           System.out.println("woman构造函数");
       }
    }
}
内部类能访问外部成员,外部类不能访问内部类成员
java 复制代码
package com.test;

public class Person {
     public int age;
     public Person(){
         System.out.println("Person构造函数");
         //name="aaa";ERROR   不能使用内部类的成员
     }
      public class Woman{
         public  String name;

       public  Woman()
       {
           System.out.println("woman构造函数");
           age=18;  //true  内部类能访问外部成员
       }
    }
}
同名的3种访问方式
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Person person=new Person();//Person构造函数
       Person.Woman woman=person.new Woman();//woman构造函数
      woman.test(20);
    //    方法参数age : 20
    //    内部类的age : 19
    //    外部类的age : 18
    }
}
java 复制代码
package com.test;

public class Person {
     public int age=18;

      public class Woman{
         public  int age=19;

         public void test(int age)
         {
             System.out.println("方法参数age : "+age);     //参数
             System.out.println("内部类的age : "+this.age);   //内部类成员
             System.out.println("外部类的age : "+Person.this.age); //外部成员
         }

    }
}

2、静态内部类

内部类为static,注意外部类不能为static
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
        Person.Woman.p();//我是静态内部类的静态方法
       Person.Woman woman=new Person.Woman();
      woman.test(20);
    //    方法参数age : 20
    //    内部类的age : 19
    //    外部类的age : 18
    }
}
java 复制代码
package com.test;

public class Person {
     public static int age=18;
     //静态内部类只能访问到外部静态内容
      public static class Woman{
         public  int age=19;
         public void test(int age)
         {
             System.out.println("方法参数age : "+age);     //参数
             System.out.println("内部类的age : "+this.age);   //内部类成员
             System.out.println("外部类的age : "+Person.age); //外部成员

         }
       public static  void p()
       {
           System.out.println("我是静态内部类的静态方法");
       }

    }
}

3、局部内部类

在方法中定义一个类
java 复制代码
package com.test;

public class Person {
     public int age;
     public void test()
     {
         class T
         {
             public void p(){
                 System.out.println("我是局部内部类");
         }
         }
      T t=new T();
         t.p();
     }
    //T t=new T();t.p();   只能在方法里面用,外部用不了
}
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Person person=new Person();
       person.test();
       //我是局部内部类
    }
}

4、匿名内部类

匿名内部类是唯一一种没有构造方法的类,它就像是直接通过 new 关键字创建出来的一个对象
一般我们可以在抽象类的对象实现中使用匿名内部类
java 复制代码
package com.test;

public abstract class Person {
     public int age;
    public void say(){}

}
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Person person=new Person(){
           @Override
           public void say() {
               System.out.println("匿名内部类");
           }
       };
        person.say();
    }
}
去掉Person的abstra关键字变为普通类依然可以使用匿名内部类
区别在于抽象类new时必须写匿名内部类,而普通类不强制。

5、lambda表达式

  • 标准格式为:([参数类型 参数名称,]...) ‐> { 代码语句,包括返回值 }
  • 和匿名内部类不同,Lambda仅支持接口,不支持抽象类
  • 接口内部必须有且仅有一个抽象方法(可以有多个方法,但是必须保证其他方法有默认实现,必须留一个抽象方法出来)
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Study study=()->System.out.println("我是study");
       //重写了接口里study抽象方法
       study.study();
    }
}
java 复制代码
package com.test;

public interface Study {
     void study();
     static void  a()
     {
     }
     default void b(){
     }
     public  static final int d=0;
}

6、方法引用

方法引用就是将一个已实现的方法,直接作为接口中抽象方法的实现
java 复制代码
import com.test.*;

public class Main {
    public static  void main(String [] argv)
    {
       Study study=Integer::sum;
       System.out.println(study.study(3,5));//8
    }
}
java 复制代码
package com.test;

public interface Study {
     int  study(int a,int b);

}
相关推荐
大麦大麦9 分钟前
深入剖析 Sass:从基础到进阶的 CSS 预处理器应用指南
开发语言·前端·css·面试·rust·uni-app·sass
hhw19911231 分钟前
c#面试题整理6
java·开发语言·c#
蠟筆小新工程師36 分钟前
Deepseek可以通过多种方式帮助CAD加速工作
开发语言·python·seepdeek
程序视点1 小时前
SpringBoot配置入门
java·spring boot·spring
Benaso2 小时前
Java,Golang,Rust 泛型的大体对比小记
java·golang·rust
程序员清风2 小时前
什么时候会考虑用联合索引?如果只有一个条件查就没有建联合索引的必要了么?
java·后端·面试
Seven972 小时前
【设计模式】掌握建造者模式:如何优雅地解决复杂对象创建难题?
java·后端·设计模式
天道有情战天下2 小时前
python flask
开发语言·python·flask
自在如风。3 小时前
MyBatis-Plus 使用技巧
java·mybatis·mybatis-plus
XORE953 小时前
IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
java·spring·intellij-idea