The inner class of java basic review

  An inner class in java exists in a java class or a method of a class.

  For the role of inner classes, it is said in the book "THINK IN JAVA": The most attractive reason for using inner classes is that each inner class can independently inherit an (interface) implementation, so no matter whether the outer class has inherited or not A certain (interface) implementation has no effect on inner classes.

My personal understanding of this sentence is: ordinary java classes can only inherit one parent class, which limits the functional expansion of java classes in a certain sense, that is, I define a java bean A, and the purpose of defining this bean must be through It implements some functions, so for this bean, I have customized some of its unique properties and functions, but in actual use, due to changes in requirements or new new requirements, I found that the bean provided The behavior does not meet my actual requirements, so I want to add some new functions on the basis of it, but in order to improve the reusability of the code, I choose to create a new B class to inherit A, so that B will have the behavior of A and Features and problems can be solved perfectly considering this. I not only improve the reusability of the code, but also add new functions to it. As long as the object of A is used, its instance is selected to be assigned with B. That's it (JAVA's polymorphism), I can have the behavior characteristics of A at the same time, and I can call the behavior of A rewritten by B. That is, the new method implementation that I modified according to the needs. (It should be noted here that the definition of java polymorphism is that if the reference of the parent class is an instance of the subclass, and the subclass overrides a method of the parent class, then when the overridden method is called, the subclass will be called. method in.)

  However, based on this question, we can extend it, what if class B needs to have the behavior and characteristics of class C at this time? For example: I need to produce a plush toy. Class A can produce its fur, eyes and decorations, but it cannot produce the inner stuffing. If I want to successfully produce a plush toy, it is bound to need to produce stuffing. However, if it is time-consuming and labor-intensive for me to master a new technology, and the benefits are not proportional to the time and energy I spend, so I choose to do it with a business I can trust, He provides the technology for the production of fillers, and I provide the technology for the exterior decoration. At this time, I need a production machine that has both my technology and the technology of the partner. Through this machine, I can successfully produce plush toys according to my requirements. But how to implement it in specific code? According to the specification of java, each class can only inherit one parent class. According to this requirement, my machine can only have my technology and cannot have two technologies at the same time. , so I need to update this machine, enrich its kernel, and add a processor to it, one processor uses my technology bean A, and the other uses the partner's technology bean C. This solved my problem. Next, we will narrow down the problem. As mentioned earlier, this machine is mine, and it already has the function of bean A. I only need to add the function of bean C to it.

  Considering this, it is very simple, I only need to declare an inner class D in B, and let it inherit C, this inner class is equivalent to the newly added processor on the machine, it is part of my machine so I allow it to access my technology, that is, D has unrestricted, unconstrained access to any of B's ​​member variables and methods, including privately declared variables and methods.

  At this point, my problem has been solved. When producing, I only need to get the instance of D in the method body produced in class B, and then use D to produce the filler. Finally, the stuffing and decoration can be processed together to produce a plush toy, paste the code below:   

 

  至此,已经简单的介绍了一下内部类的一个简单的作用,我们再继续往下扩展这个问题,此时有另外一个厂家生产其它的毛绒玩具,也需要生产填充物,它跟我们的情况一样,幸运的是我们手上有可以信得过的商家来与我们合作生产,但是它并没有,所以他找到了我,想借我的机器过去生产填充物,我当然不会傻乎乎的把技术给他,所以我提出了一个条件,我可以帮你生产填充物,但是只能用我的机器来生产,这样一来,我既帮了别人,又保护了我的技术产权。对于java来说当然也会考虑到这一点,所以对于外部想拿到内部类实例的情况便有了定义:必须通过外围类的实例来生成内部类的实例。                              

   当然 对于这一点的定义的原因,肯定也不会这么简单;但是也没有深究下去的必要。此外,前文说到内部类可以无限制的方位外围类的成员变量以及行为方法,虽然在此例子中没有体现出来,但是有一点需要注意,如果内部类有同名方法必须使用"OuterClass.this.MethodName()"格式调用(其中OuterClass与MethodName换成实际外部类名及其方法;this为关键字,表示对外部类的引用);若内部类无同名方法可以直接调用外部类的方法。
        但外围类无法直接调用内部类的private方法,外部类同样无法直接调用其它类的private方法。注意:内部类直接使用外部类的方法与该方法的权限与是否static无关,它取决于内部类是否有同名方法。

 

 其实使用内部类最大的优点就在于它能够非常好的解决多重继承的问题,但是如果我们不需要解决多重继承问题,那么我们自然可以使用其他的编码方式,但是使用内部类还能够为我们带来如下特性(摘自《Think in java》):

      1、内部类可以用多个实例,每个实例都有自己的状态信息,并且与其他外围对象的信息相互独立。

      2、在单个外围类中,可以让多个内部类以不同的方式实现同一个接口,或者继承同一个类。

      3、创建内部类对象的时刻并不依赖于外围类对象的创建。

      4、内部类并没有令人迷惑的“is-a”关系,他就是一个独立的实体。

      5、内部类提供了更好的封装,除了该外围类,其他类都不能访问。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845528&siteId=291194637