当前位置: 首页 > news >正文

JavaSE:11、内部类

学习 资源1

学习资源 2

1、成员内部类

import com.test.*;public class Main {public static  void main(String [] argv){Person person=new Person();//Person构造函数Person.Woman woman=person.new Woman();//woman构造函数}
}
package com.test;public class Person {public Person(){System.out.println("Person构造函数");}public class Woman{public  Woman(){System.out.println("woman构造函数");}}
}
内部类能访问外部成员,外部类不能访问内部类成员
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种访问方式
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}
}
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 
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}
}
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、局部内部类

在方法中定义一个类
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();   只能在方法里面用,外部用不了
}
import com.test.*;public class Main {public static  void main(String [] argv){Person person=new Person();person.test();//我是局部内部类}
}

4、匿名内部类

匿名内部类是唯一一种没有构造方法的类,它就像是直接通过 new 关键字创建出来的一个对象
一般我们可以在抽象类的对象实现中使用匿名内部类
package com.test;public abstract class Person {public int age;public void say(){}}
import com.test.*;public class Main {public static  void main(String [] argv){Person person=new Person(){@Overridepublic void say() {System.out.println("匿名内部类");}};person.say();}
}
去掉Person的abstra关键字变为普通类依然可以使用匿名内部类
区别在于抽象类new时必须写匿名内部类,而普通类不强制。

5、lambda表达式

  • 标准格式为:([参数类型 参数名称,]...) ‐> { 代码语句,包括返回值 }
  • 和匿名内部类不同,Lambda仅支持接口,不支持抽象类
  • 接口内部必须有且仅有一个抽象方法(可以有多个方法,但是必须保证其他方法有默认实现,必须留一个抽象方法出来)
import com.test.*;public class Main {public static  void main(String [] argv){Study study=()->System.out.println("我是study");//重写了接口里study抽象方法study.study();}
}
package com.test;public interface Study {void study();static void  a(){}default void b(){}public  static final int d=0;
}

6、方法引用

方法引用就是将一个已实现的方法,直接作为接口中抽象方法的实现
import com.test.*;public class Main {public static  void main(String [] argv){Study study=Integer::sum;System.out.println(study.study(3,5));//8}
}
package com.test;public interface Study {int  study(int a,int b);}

http://www.lryc.cn/news/440481.html

相关文章:

  • VTD激光雷达(7)——07_OptiX_Variables_Advanced
  • 运维工程师面试整理-自动化运维
  • 【JAVA基础】实现Tomcat基本功能
  • 风力发电叶片缺陷检测数据集
  • 数据类型自动转换的解决方案
  • 大厂校招:唯品会Java面试题及参考答案
  • Qt常用控件——QLCDNumber
  • 专业学习|GERT网络概览(学习资源、原理介绍、变体介绍)
  • 搭建一个基于角色的权限验证框架
  • 下载chromedriver驱动
  • 在STM32工程中使用Mavlink与飞控通信
  • 【Elasticsearch】-7.17.24版本接入
  • ShouldSniffAttr在自动化测试中具体是如何应用?
  • 前端vue3打印,多页打印,不使用插件(工作中让我写一个打印功能)
  • 传感技术是如何实现实时监测和控制的呢
  • 为什么mac打不开rar文件 苹果电脑打不开rar压缩文件怎么办
  • linux下日志系统setvbuf接口及结构体 handle_file_t成员介绍
  • ESP8266+httpServer+GET+POST实现网页验证密码
  • git仓库修改ip,本地代码修改
  • 轻便灵活,声学卓越,流动会场创新应用—轻空间
  • 13 Midjourney从零到商用·进阶篇:灯光、角度与风格等精细控制方法
  • 为什么要把raw转成yuv
  • 基于双向RRT算法的三维空间最优路线规划matlab仿真
  • 若依-原理
  • 台球厅灯控系统如何布线 佳易王桌球计时计费管理系统操作教程
  • 安卓将本地日志上传到服务器
  • FloodFill(洪水灌溉)算法专题——DFS深搜篇
  • 直播标准权威发布,阿里云RTS获首批卓越级评估认证
  • iOS 知识点记录
  • C++系列-STL中搜索相关算法