Java inner class

Covering hands is cloud p 2021-01-20 22:03:38
java inner class


One 、 Definition

1、 Another class defined inside a class , The inner class has direct access to all variables inside the outer class , To keep communication with external class Links ; Inner classes provide some kind of container into their outer classes .
2、 A big reason to use inner classes is : Inner classes can independently inherit from the implementation of an interface , Whether or not the external class has inherited , No effect on inner classes . This indirectly implements the inheritance of multiple classes .
3、 Inner classes can be in a class 、 Methods are defined in any scope
4、 In another class, instance a class's non static inner class using .new. It is impossible to create an internal class object before having an external class object , So we need to implement the exception class first . If the inner class is static , It's the same as a normal static class , Can be called directly .
5、 Inner classes in interfaces , Internal classes can be placed in interfaces , Any class in the interface is automatically public static Of , So it's equivalent to placing a static Public methods ;
6、 Inner classes are object-oriented closures
7、 Inner classes can be inherited , When inheriting, you don't need to inherit external classes ;
8、 After the inner class is compiled, a separate .class file , The naming rule is : The name of the external class plus $, Plus the name of the inner class . If the inner class is anonymous , The compiler simply generates a number as its identifier .
Example :

public class StudyTest {
StudyTest(){
System.out.println("construct StudyTest");
}
private String study = "study";
public class InnerClass{
public StudyTest outClass(){
System.out.println(study);
return StudyTest.this; // Use .this Call the parent class of the current class directly 
 }
}
public static void main(String[] args) {
InnerClass innerClass = new StudyTest().new InnerClass(); // Instance inner class 
 innerClass.outClass();
}
}

Running results :

construct StudyTest
study

Two 、 application

1、 Inner classes are used to hide implementation details

Interface and call :

public interface StudyInterface {
String getValue();
}
public class MainClass {
public static void main(String[] args) {
StudyInterface studyInterface = new StudyTest().instance();
System.out.println(studyInterface.getValue());
}
}

Realization :

public class StudyTest {
StudyTest(){
System.out.println("construct StudyTest");
}
private String study = "study";
// This inner class is not visible to the outside , No instance, no inheritance .
private class InnerClass implements StudyInterface{
@Override
public String getValue() {
return study;
}
}
public StudyInterface instance(){
return new InnerClass();
}
}

In this way, any type dependent encoding can be completely blocked , And completely hide the implementation details . From the caller's point of view , Due to the lack of access to any newly added 、 Methods that are not originally public interfaces , So there's no value in extending interfaces .

2、 Anonymous inner class

public class StudyTest {
private String name = "study";
public String getName(){return this.name;}
}
public class MainClass {
public StudyTest studyTestPlus(){
return new StudyTest(){
private String nameplus = "studyTestPlus";
public String getName(){return nameplus;}
}; // Notice the semicolon here , Don't omit 
 }
public static void main(String[] args) {
StudyTest studyTest = new MainClass().studyTestPlus();
System.out.println(studyTest.getName());
}
studyTestPlus() Place is equivalent to StudyTest Class is extended by inheritance , The effect is the same as the code below :
public class StudyTestPlus extends StudyTest{
private String nameplus = "studyTestPlus";
public String getName(){return nameplus;}
}
public StudyTest studyTestPlus(){
return new StudyTestPlus();
}

Anonymous inner classes can extend classes , You can also implement interfaces , But not both , If you implement an interface, you can only implement one .

3、 Anonymous inner classes combine with factory patterns

public interface Service{ void printName();}
public interface ServiceFactory{Service getService();}
public class Factory{
public static void serviceConsumer(ServiceFactory factory){
factory.getService().printName();
}
}
public class Implements1 implements Service{
public void printName(){System.out.println("Implements1");}
public static ServiceFactory factory =new ServiceFactory() {
public Service getService() {
return new Implements1();
}
};
}
public class Implements2 implements Service{
public static ServiceFactory factory =new ServiceFactory() {
public Service getService() {
return new Implements2();
}
};
}
// Use 
public static void main(String[] args) {
Factory.serviceConsumer(Implements1.factory);
Factory.serviceConsumer(Implements2.factory);
}

 

版权声明
本文为[Covering hands is cloud p]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210120220254916i.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云