Java foundation Past 3

Internal class: better achieve hiding information; in Java, a class may be defined inside another class or a method which, such a class called internal type, such class is called internal type; corresponding, comprising inner class class is called an external class.

Internal categories:

  • Members of the inner class
  • Static inner classes
  • Method inner class
  • Anonymous inner classes

Members of the inner class: Also called common inner class

class Outter{
    
    private Inner inner = null;
    
    public Inner getInnerInstance(){
        if(inner == null)
            inner = new Inner();
        return inner;
    }

    class Inner{//成员内部类
        public Inner(){
            
        }
    }    

}

Acquisition method within the class members:

  1. External new class inner class .new
  2. External object internal category .new
  3. External object. The method of obtaining

Members inner classes: an inner class when used outside, can not be directly instantiated, it needs an external class information to complete the instantiation; 2 internal class can member access outer class; 3 if the same name attribute is present, priority access. internal class defined; 4 inside the outer class access class information, required by internal class instance can not directly access; 5 internal access modifier classes can be any, but is affected by the extent of access;. 6 may use an external class . .this way the members of the class of the same name to access external information; 6 after an internal class compiled .class file naming: external internal class $ class .class; 7 inner class can contain methods with the same signature method outside class how inner class call external class: external class name .this method.

Static inner classes: static inner classes modified

class Outter{
    
    public Outter(){

    }

    static class Inner{//静态内部类
        public Inner(){

        }
    }
}

1. static inner class object can not depend on the external object, created directly; 2 static inner classes, static members can directly access the outer class, if you need to call non-static member can be invoked through an object instance; 3 to create static internal class object instances, without depending on the external object; 3 inside the outer class by class static members, access to the internal static class member... 4. If the internal and the external class attribute class attribute of the same name, the default internal direct call class members. The need to access external static property class, the class may be external. Manner property. The need to access the external non-static property class by class new external (). Method property.

Method inner classes: class outside the class defined internal method, it has become a local inner class.

class People(){
    
    public People(){
    
    }
}

class Man{
    public Man(){
        
    }

    public Peopele getWoman(){
        class Woman extends People{//局部内部类
            int age = 0;
        }
        return new Woman();
    }
}

Local variables defined in the method can only be used in the method, the method can not be defined within a static member.

Method inner classes: a method defined in the interior, a method is also scope; 2 and a method of using rules as internal members, can not be added in front of class public, private, protected, static; 3 can not contain a static class member;. 4. the class may comprise final, abstract modified member.

Anonymous inner classes; no name inside the class (only once), it will create a class definition of class, put together complete.

public class Demo {
	
	public static void main(String []args) {
		Thread t = new Thread() {//Thread类匿名内部类
			public void run() {
				for(int i = 1; i <= 5; i++) {
					System.out.print(i + " ");
				}
			}
		};
		t.start();
	}

}

scenes to be used:

  1. Use only one instance of the class
  2. After the class definitions used immediately
  3. To the class name does not cause the code to be more easily understood

Precautions:

  1. Compiled files named: external digital class $ .class
  2. Can not use public, private, abstract, static modification, anonymous inner classes can not appear abstract methods
  3. The method of construction can not be prepared, may be added to code block configured
  4. Static members can not appear
  5. Anonymous inner classes can implement interfaces can inherit the parent class, but can not have both
Published 20 original articles · won praise 8 · views 4454

Guess you like

Origin blog.csdn.net/Ally441/article/details/104349970