Define an external class Father, members of variable name and assign an initial value.

1, the use of inner classes:
(1) a defined external Father class, members of the variable name and assign an initial value.
(2) Child define an inner class, and define a getValue () method call an external name class Father of variables in the process.
(3) define a test class, create an object in the main method to test the Child class, and calls the getValue () method

 

Father.java 

Package com.fs.test; 

class Father {
     Private String name = "tiedan" ;
     class Child {   // define an inner class 
         public  void the getValue () {   // definition of a general method 
            System.out.println (name) ; // call attribute name 
        } 
    } 
    // definition of a class in an external method, which is responsible for generating an internal class object and call getValue () method. 
    public  void getValue () { 
        Child in = new new Child (); 
        in.getValue ( ); 
        } 
} 


Test.java 

Package com.fs.test;

public  class the Test {
     public  static  void main (String [] args) { 
        Father Out = new new Father (); // external object 
        Out.getValue (); // external class method 
    } 
}

 

Guess you like

Origin www.cnblogs.com/ooo888ooo/p/11105181.html