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);

}
相关推荐
DIY机器人工房25 分钟前
一个基于 epoll 实现的多路复用 TCP 服务器程序,相比 select 和 poll 具有更高的效率
开发语言·嵌入式硬件·php·嵌入式·diy机器人工房
Ice__Cai37 分钟前
Python 基础详解:数据类型(Data Types)—— 程序的“数据基石”
开发语言·后端·python·数据类型
_herbert39 分钟前
MAVEN构建分离依赖JAR
java
野犬寒鸦1 小时前
Pipeline功能实现Redis批处理(项目批量查询点赞情况的应用)
java·服务器·数据库·redis·后端·缓存
lilv661 小时前
python中用xlrd、xlwt读取和写入Excel中的日期值
开发语言·python·excel
꧁༺摩༒西༻꧂1 小时前
Spring Boot Actuator 监控功能的简介及禁用
java·数据库·spring boot
Java中文社群1 小时前
快看!百度提前批的面试难度,你能拿下吗?
java·后端·面试
丨千纸鹤丨2 小时前
Tomcat
java·tomcat
发发发发8882 小时前
leetcode 674.最长连续递增序列
java·数据结构·算法·leetcode·动态规划·最长连续递增序列
阿巴~阿巴~2 小时前
构造函数:C++对象初始化的核心机制
开发语言·c++