Java Inner class action

Source: Internet
Author: User
Tags closure modifier

Recommendation one, define

The class that is placed inside a class is called the inner class.

Second, the role

1. Internal classes can be very good to implement the hidden

The generic non-intrinsic class is not allowed to have private and protected permissions, but the inner class can

2. Inner class has access to all elements of the perimeter class

3. However, multiple inheritance is achieved

4. You can avoid modifying the interface and implementing the invocation of two methods of the same name in the same class.

Iii. examples

1. Implement hidden

Usually our access to the class is restricted by the access modifier in front of the class, the general non-inner class is not allowed to have private and protected permissions, but the inner class can, so we can hide our information through the inner class. You can look at the following example

Interface

Package insidecategory; public interface interfacetest{void Increment ();}

Specific class

Package insidecategory; public class Example {      private class Insideclass implements Interfacetest    {public         void Test ()         {
    system.out.println ("This is a Test");         }    }    Public interfacetest Getin ()    {        return new Insideclass ();}    }

  

The bold part above is the inner class, the access modifier is private

Client programs

Package insidecategory; public class Testexample {public  static void Main (String args[]) {    Example a=new Example ();    Interfacetest A1=a.getin ();    A1.test (); }}

  

The bold part is the code that the client calls, and from this code I only know example's

The Getin () method can return an instance of interfacetest but I do not know how this instance is implemented. And because Insideclass is private, we don't see the name of the specific class without looking at the code, so it can be very well hidden.

2. Unconditional access to all elements of the perimeter class

Package insidecategory; public class Tagbean {  private String name= "Liutao";   Private class Intest   {public      intest ()      {          System.out.println (name);      }   }   public void Test ()   {    new intest ();   }   public static void Main (String args[])   {       Tagbean bb=new tagbean ();       Bb.test ();   }}

  

Look at the bold section above, name this variable is a private variable defined inside the Tagbean. This variable provides unconditional access to the SYSTEM.OUT.PRINTLN (name) in the inner class;

3. Multiple inheritance can be achieved

is one of the most important reasons why an internal class exists. It is because of his existence that the inheritance mechanism of Java is more perfect. We all know that Java can inherit only one class, and its multiple inheritance is implemented by interfaces before we learn the inner class. But using interfaces sometimes has a lot of inconvenient places. For example, if we implement an interface, we must implement all the methods inside it. And the inner classes are different. It allows our class to inherit multiple concrete classes or abstract classes. Let's look at the following example.

Class One

Package insidecategory; public class Example1 {public    String name ()   {       return ' Liutao ';   }}

  

Class Two

Package insidecategory; public class Example2 {public     int age ()    {        return;}    }

  

Class Three

Package insidecategory; public class mainexample{   private class Test1 extends Example1    {public        String name ()        {          return Super.name ();        }    }    Private class Test2 extends Example2    {public       int age ()       {         return super.age ();       }    }   Public String name ()    {    return new test1 (). Name ();   }   public int Age ()   {       return new test2 (). age ();   }   public static void Main (String args[])   {       mainexample mi=new mainexample ();       System.out.println ("Name:" +mi.name ());       System.out.println ("Age:" +mi.age ());}   }

  

Attention to see class three, the inside of the implementation of the two inner class test1, and Test2, Test1 class and inherited Example1,test2 inherited Example2, In this way our class three mainexample has the methods and properties of Example1 and Example2, and it also implements multiple inheritance indirectly.

Iv. avoid modifying interfaces and implementing the invocation of two methods of the same name in the same class.

Let's imagine if your class inherits a class and implements an interface, but you realize that there are two methods of the same name in the class and interface you inherit? How do you differentiate them?? This requires our inner class. Look at the code below.

Interface

Package insidecategory; public interface incrementable{void Increment ();}

  

Class Myincrement

Package insidecategory; public class Myincrement {public     void increment ()    {      System.out.println ("Other increment ()");    }    static void F (myincrement f)    {        f.increment ();    }}

  

Everyone looks at the black part above, two methods are the same. Look at the following class to inherit these two classes

If you don't use an inner class

Package insidecategory; public class Callee2 extends myincrement implements incrementable{public void increment ()      {        //code       }}

  

Want to ask everybody increment () This method belongs to cover myincrement here method? Or incrementable the method here. How can I transfer to myincrement here? Obviously, this is a bad distinction. And if we use internal classes, we can solve this problem very well. Look at the code below

Package insidecategory; public class Callee2 extends myincrement{private int i=0; private void incr () {       i++;       System.out.println (i); } Private class Closure implements incrementable {public      void increment ()      {        incr ();}      } Incrementable getcallbackreference () {      return new Closure ();}}

  

We can implement interfaces with internal classes so that they do not conflict with the methods of the perimeter class.

Java Inner class action

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.