Home  >  Article  >  Java  >  Inner classes in java (basic)

Inner classes in java (basic)

炎欲天舞
炎欲天舞Original
2017-08-04 09:36:131123browse

Classification of internal classes: regular internal classes, static internal classes, private internal classes, local internal classes, and anonymous internal classes.

Example 1: Regular internal class

//外部类
class Out {    
  private int age = 12;        
  //内部类    
  class In {        
    public void print() {            
      System.out.println(age);       
    }    
  }
} 
public class Demo {    
  public static void main(String[] args) {        
    Out.In in = new Out().new In();        
    in.print();        
    //或者采用下种方式访问        
    /*        
    Out out = new Out();        
    Out.In in = out.new In();        
    in.print();        
    */    
  }
 }

Running result:12

It is not difficult to see from the above example that internal classes actually seriously damage the good code structure, but why use inner classes?

Because the inner class can freely use the member variables of the outer class (including private) without generating objects of the outer class, this is the only advantage of the inner class

Just like the heart can directly access the blood of the body , instead of drawing blood through a doctor

After the program is compiled, two .class files will be generated, namely Out.class and Out$In.class

where $ Represents the one in Out.In in the above program.

Out.In in = new Out().new In() can be used to generate objects of inner classes. There are two small knowledge points in this method. Need to pay attention

 1. The Out at the beginning is to indicate which external class the internal class object that needs to be generated is in

 2. There must be an object of the external class before the object of the internal class can be generated, because The function of the inner class is to access the member variables in the outer class

Example 2: Variable access form in the inner class

class Out {
    private int age = 12;
     
    class In {
        private int age = 13;
        public void print() {
            int age = 14;
            System.out.println("局部变量:" + age);
            System.out.println("内部类变量:" + this.age);
            System.out.println("外部类变量:" + Out.this.age);
        }
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Out.In in = new Out().new In();
        in.print();
    }
}

Running results:

Local variables: 14
Internal class variables: 13
External class variables: 12

It can be found from Example 1 that the internal class does not have member variables and local variables with the same name In this case, the inner class will directly access the member variables of the outer class without specifying the Out.this. attribute name

Otherwise, the local variables in the inner class will overwrite the member variables of the outer class

To access the member variables of the inner class itself, you can use this. attribute name, and to access the member variables of the external class, you need to use the Out.this. attribute name

Example 3: Static inner class

class Out {
    private static int age = 12;
     
    static class In {
        public void print() {
            System.out.println(age);
        }
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Out.In in = new Out.In();
        in.print();
    }
}

Run results: 12

You can see that if you use static to make the internal static, then the internal class can only access the static member variables of the external class, which has limitations

Secondly, because the inner class is static, Out.In can be viewed as a whole, and you can directly new the object of the inner class (access static through the class name, it does not matter whether the outer class object is generated or not)

Instance 4: Private inner class

class Out {
    private int age = 12;
     
    private class In {
        public void print() {
            System.out.println(age);
        }
    }
    public void outPrint() {
        new In().print();
    }
}
 
public class Demo {
    public static void main(String[] args) {
        //此方法无效
        /*
        Out.In in = new Out().new In();
        in.print();
        */
        Out out = new Out();
        out.outPrint();
    }
}

Running results:12

If an inner class only wants to be operated by methods in the outer class, you can use private to declare the inner class

In the above code, we must generate an object of the In class in the Out class for operation, and we can no longer use Out.In in = new Out().new In() to generate an object of the inner class

In other words, the inner class at this time can only be controlled by the outer class

Like, my heart can only be controlled by my body, and others cannot directly access it

Instance 5: Local inner class

class Out {
    private int age = 12;
 
    public void Print(final int x) {
        class In {
            public void inPrint() {
                System.out.println(x);
                System.out.println(age);
            }
        }
        new In().inPrint();
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Out out = new Out();
        out.Print(3);
    }
}

Running result:

3
12

In the above code, we moved the inner class to method of the external class, and then generate an internal class object in the method of the external class to call the internal class method

If we need to pass parameters into the method of the external class at this time, then the method of the external class Formal parameters must be defined using final

As for final, it has no special meaning here, it is just a form of expression

The above is the detailed content of Inner classes in java (basic). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn