day(9)The Inner Class

One class only in a project,but we still need redefine a class in a defined class

reason:
We wish to encapsulate a function which we don't want it exposed to the outside
And then manage this function or data uniformly.


public class MyClass{
    //The car exists first,then the engine
    Car bz= new Car();
    //We rarely use this way.
    Car.Engine engine = bz.new Engine();
    //Invoking the static inner class
    //We use this more.
    Car.Color color = new Color();
}

The inner class is the member of the external class
It can access the memers of the external class,but vice versa(反之亦然)
It can use the modifiers : public,private,protected,final,but the outer class can only
use public
It can be common inner class(use object to invoke the members),or static inner class(use class name to invoke the static members.

public class Car{
//compared with the inner class explaining the status of it.
  private int wheels;

private void start(){
     wheels = 4;
      // use the inner class inside
      Engine engine = new Engine();
    }

      public static class Color{
          private String color;
}
      public class Engine{
          public int a;
       public engine(){
          wheels = 5;
          start();
     }
  }
}

你可能感兴趣的:(day(9)The Inner Class)