24 using inner class java object oriented (InnerClass) of

/*

  • Using the internal structure of the class of class members
  • 1, Java can be defined in a class A, class B, class A external class, B class internal,
  • Note that inside the class name of class B must be unique outer class A
  • Members of the inner class compiler generates external internal class name class name $ .class bytecode files later
  • Partial inner classes compiled class name $ external digital internal name class bytecode files after .class (plus numbers in order to prevent the same name)
  • 2, according to the position inside the class declaration is divided into:
  • Members of the inner class (static and non-static)
  • 1, as a member of the outer class
  • You can call external class structure

  • Unlike external static class can not be modified, the internal class can be declared static.

  • Unlike external class can only be used or the default public authority, the internal class can use private and protected modified

  • 2, as a class
  • The class may have attributes, methods, constructors, code blocks, inner class

  • Class can be inherited, it can also be modified with a final statement can not be inherited

  • Inner class is an abstract class, abstract can be modified

  • 3, members of the inner class use:
  • 3.1 create variables inside the class members
  • Internal static class object: External Internal class name class name external variable name = new class name inside the class name ();.
  • Non-static inner class object: instantiate an object requires the use of an external call to create an external class class name inside the class name instance variable name = external object name .new inner class name ();.
  • 3.2 members of the inner class call external class structure
  • Call with the same name as the reference method:

  • The external name class name .this method ();
  • . This method name ();
  • When there is no direct use of the same method on the name (); can automatically identify the manner
  • Calls the same attributes:

  • The external name of .this class property name ();
  • . This property name ();
  • Directly in the absence of the same name attribute name can automatically identify ways and means.
  • Note the use of members of the inner class 3.3
  • Non-static members inside the class members can not be declared as static, and only be declared static members in class or outside class members inside the static. (From members of the life-cycle point of view, we need to be consistent)

  • Members of the class to access external members of the inner class, we need to "inner class. Members" (static inner classes) or "inner class object. Members" (non-static inner classes) way

  • Members of the inner class can be used as external members of all classes, including private data

  • When you want to use the internal part of the external static member class in the class, consider the inner class declared as static (internal structure of the external class as a class, and attribute similar methods, need to be static can be called a static method of the outer class )

  • Summary: The members of the inner class compared to the outer class can use more than two modifiers permission, can be declared as static, limited to non-static inner classes can not have static structure.
  • 4, the local internal class (defined in the constructor, the method body code block, the statement after the first use. Anywhere other than the class can not be used)
  • 4.1 Return value inside the object class may be a local external use method returns, the return type can only be partial internal super class or interface type parent
  • 4.2 Internal partial classes can use an external member of the class, including private.
  • 4.3 local inner class and local variables similar position, you can not use public, protected, default, private
  • 4.4 Local inner classes can not use static modification, and therefore can not contain static members
  • 4.5 local internal local variables that are outside the class methods, but must be final. The life cycle of local and inner classes due to different local variables.
  • 4.6 partial inner class declared final and abstract of
  • Summary: Local inner classes rarely used, more restrictions than members of the inner class. Typically used in the realization of an interface. Internal only need to rewrite abstract methods interface.
  • 5, an anonymous inner classes
  • 5.1 anonymous inner classes can not define any static members, methods and classes, can only create an instance of an anonymous inner class. An anonymous inner classes must be behind the new, which implicitly implemented using an interface or to implement a class.
  • 5.2 anonymous inner class must inherit the parent class or implement an interface
  • 5.3 anonymous inner classes only one object, the object reference only polymorphic form
  • 5.4 anonymous inner classes using the same format and use of the parent class and anonymous classes interface.

*/

package object_chapter2;

public class Object_InnerClass {
	static int number = 10;
	int arguments = 20;
	

	public static void main(String[] args) {
		//创建静态成员内部类对象
		Vehicle.Engine e = new Vehicle.Engine();
		System.out.println("发动机有" + e.cylinderNumber + "个汽缸");
		new Vehicle.Engine().work();
		//创建非静态成员内部类对象
		Vehicle v = new Vehicle();
		Vehicle.Room r = v.new Room();//或者v.new Vehicle.Room()
		System.out.println("车里有" + r.chair + "个座位");
		r.reName("车名:五菱之光");
		new Vehicle().new Room().set();	
		v.getInstance().compare(v);
		
	}
		
	public static void method() {
		
		int num = 11;
		//方法体内的局部内部类
		 class InnerClass{
			void show() {
//				num = 12;
				System.out.println(num);//局部内部类的方法中可以调用内部类所在方法的局部变量。不能修改
				//在JDK1.7及以前,局部变量想要被局部内部类调用必须显式的被声明为final类型的,JDK1.8后会自动识别为final的。
				//由于局部内部李生成的字节码文件是独立的,外部类中的变量想要被内部类使用只能是常量,内部类使用的是外部类变量的副本
				//只能使用,无法修改。
			}
			
		}
//		 num++;//final的变量不能修改,否则局部内部类调用时会报错。
	}
	
    public Object_InnerClass() {
    	//构造器中定义的局部内部类
		 class InnerClass{
			
		}		
	}
    
    {
    	//代码块中定义的局部内部类
    	 class InnerClass{
    		
    	}
    }
    
	//静态内部类
	private static final class InnerClass1{
		void method() {
		number = 110;
//		Object_InnerClass.this.arguments = 20;//静态内部类中无法调用非静态结构
	 }
	}
	
	//非静态内部类
	protected class InnerClass2{
		void method() {
			Object_InnerClass.number = 110;
			Object_InnerClass.this.arguments = 20;//非静态内部类中可以调用非静态结构
			}
	}
	
	class InternalClass{
		//内部类中也可以嵌套一个内部类
		class InnerInternalClass{			
		
	    }
	}		
}

class Vehicle{
	static int wheelNumber = 4;
	String name = "号称秋名山之神";
	
	void run() {
		System.out.println("可以在各种路况上跑");
	}
	//静态成员内部类
	static class Engine{
		int cylinderNumber = 4;
		void work() {
			System.out.println("输出200匹马力的动力");
		}
				
	}
	//非静态成员内部类
	class Room{
		//static int chair = 4;//非静态内部类不允许使用静态结构
		int chair = 7;
		String name = "内部空间极大";
		void set() {
			Vehicle.this.run();//内部类可以调取外部类的结构
			carry();//不重名的情况下可以省略前面的部分
			System.out.println("可以坐很多个人");
		}
		
		void reName(String name) {
			System.out.println(name);//调用方法形参
			System.out.println(this.name);//调用内部类属性
			System.out.println(Vehicle.this.name);//调用外部类同名属性
		}
	}
	
	   void carry() {
		System.out.println("能拉人能载货");
		
	}
	//局部内部类的常见场景,方法返回值是一个接口或者抽象类
	 Comparable getInstance() {
		 //创建一个内部类为借口的实现类
		class MyCompareable implements Comparable{

			@Override
			public void compare(Vehicle v) {
				System.out.println("不管什么车都挡不住五菱的光辉!!");				
			}
			
		}
		//返回内部类的实例
//		return new MyCompareable();	
		//方式二,使用匿名内部类
		return new Comparable() {
			
			@Override
			public void compare(Vehicle v) {
				System.out.println("天下无敌");				
			}
		};						
	}				 
}

interface Comparable{
	 void compare(Vehicle v);
}
Published 47 original articles · won praise 1 · views 1045

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104455753