java object-oriented class inside a 22

1. Internal class concept

  • What is the inner class?

    The class written inside the other classes, can be written in the other class members and the local position of the position, then write in the other class is called the interior of the inner class category (built-in classes, nested classes), also referred to other types of external class.

  • When using the internal class?

    In describing things, if an internal matter further comprises other things may contain, for example, in the description of automobiles, automobile further includes an engine that, when the internal engine can be used to describe the class.

  • What kind of internal classification?

Internal divided into categories: internal class members, local inner classes, anonymous inner classes and static inner classes.

  • Compilation features inner class?

    OuterClass.class and OuterClass $ InnerClass.class: For a class called OuterClass internal and external class called InnerClass, and after a successful compilation, so there will be two class files.

  • Features access inner class?

    Inner class can directly access the external members of the class, outside class to access the inner class, you must establish internal class object.

2. The members of the inner class

Internal class members, the members define a position outside the class. The class member variable is similar, can be accessed through the external object.

  • Definition Format
class 外部类 {
	修饰符 class 内部类 {
		// 其它代码
	}
}

[Example] members of the inner class code demonstrates

// 外部类
class OuterClass {
	// 成员内部类
	public class InnerClass {
		// 成员内部类的成员变量
		String name;
	    // 成员内部类的构造方法
		public InnerClass() {} // 无参构造方法
		public InnerClass(String name) { // 有参构造方法
			this.name = name;
		}		
		// 成员内部类的成员方法
		public void test() {
			System.out.println("innerClass test()");
		}
	}
	// 外部类成员方法
	public void show() {
		// 实例化成员内部类
		InnerClass in = new InnerClass("小明");
		// 获取成员内内部类的属性
		System.out.println(in.name); // 输出:小明
		// 调用成员内内部类的方法
		in.test();
	}
}
// 测试类
public class InnerClassDemo {
	public static void main(String[] args) {
		new OuterClass().show();
	}
}
  • Inner class access methods

The external name class class name inside the external variable name = new class name (argument list) .new internal name class (the argument list);

[Example] inner class access code demonstrates the way

// 外部类
class OuterClass {
	String sex = "娜娜";
	// 成员内部类
	public class InnerClass {
		// 成员内部类的成员变量
		String name;
	    // 成员内部类的构造方法
		public InnerClass() {} // 无参构造方法
		public InnerClass(String name) { // 有参构造方法
			this.name = name;
		}
		// 成员内部类的成员方法
		public void test() {
			System.out.println("innerClass test()");
		}
	}
}
// 测试类
public class InnerClassDemo {
	public static void main(String[] args) {
		/* 
		 * 实例化内部类完整写法
		 * OuterClass outer = new OuterClass();
		 * OuterClass.InnerClass in = outer.new InnerClass();
		 */
		OuterClass.InnerClass in = new OuterClass().new InnerClass("小明"); 
		// 获取成员内内部类的属性
		System.out.println(in.name); // 输出:小明
		// 调用成员内内部类的方法
		in.test();
	}
}
  • Members of the inner class Precautions
  1. Static method of the outer class members can not access internal variables.

  2. Any static variables and static methods can not exist member of the inner class.

  3. Members of the inner class is attached to the periphery of the class, so only create a class to be able to create an internal peripheral class.

  4. External access class member variables: External .this class member variables, class members to access external method:.. .This outside the class member method.

[Example] members of the inner class code demonstrates Precautions

// 外部类
class OuterClass {
	String name = "娜娜";
	// 成员内部类
	public class InnerClass {
		// 成员内部类的成员变量
		String name;
	    // 成员内部类的构造方法
		public InnerClass() {} // 无参构造方法
		public InnerClass(String name) { // 有参构造方法
			this.name = name;
		}
		// 成员内部类的成员方法
		public void show(String name) {
			// 局部变量,成员内部类成员变量,外部类成员变量同名时的访问
			System.out.println("局部变量:" + name);
			System.out.println("成员内部类成员变量:" + this.name);
			System.out.println("外部类成员变量:" + OuterClass.this.name);
		}
		// 2、成员内部类中不能存在任何的静态变量和静态方法。
		// static String company = "北京尚学堂"; 编译错误
		// public static test() {} 编译错误
		
	}
	// 外部类的静态方法
	public static void show() {
		// 1、外部类的静态方法中不能访问成员内部变量。
		// InnerClass in = new InnerClass(); 编译错误
	}
}
// 测试类
public class InnerClassDemo {
	public static void main(String[] args) {
		OuterClass.InnerClass in = new OuterClass().new InnerClass("小明"); 
		// 获取成员内内部类的属性
		System.out.println(in.name); // 输出:小花
		// 调用成员内内部类的方法
		in.show("小花");
	}
}

3. Local inner class

Internal local class that defines the local position outside the class method. And accessing local variables in a method similar to the method can be accessed by calling

  • Definition Format
class 外部类 {
	修饰符 返回值类型 方法名(参数列表) {
		class 内部类 {
			// 其他代码
		}
	}
}

[Example] partial inner class code demonstrates

// 外部类
class OuterClass {
	// 外部类的方法
	public void test() {
		// 局部内部类
		class InnerClass {
			// 局部内部类的成员变量
			public String name;
		    // 局部内部类的构造方法
			public InnerClass() {} // 无参构造方法
			public InnerClass(String name) { // 有参构造方法
				this.name = name;
			}
			// 局部内部类的成员方法
			public void show() {
				System.out.println("局部内部类中的show方法");
			}
		}
		
		// 实例化局部内部类,注意只能在对应的局部作用域中实例化!!!
		InnerClass in = new InnerClass("小明");
		// 获取局部内部类中的成员变量
		System.out.println("name:" + in.name);
		// 调用局部内部类中的成员方法
		in.show();
	}
}
  • Precautions partial inner class
  1. Partial internal class can be instantiated in the corresponding local scope.

  2. Any static variables and internal methods can not exist in the local class.

  3. Local inner class is attached to the periphery of the class, so only create a class to be able to create an internal peripheral class.

  4. External access class member variables: External .this class member variables, class members to access external method:.. .This outside the class member method.

  5. External local variables local inner class with a final visit must be modified to prevent changes outside a local variable. After JDK1.8 you do not have to add final.

[Example] partial inner class code demonstrates Precautions

// 外部类
class OuterClass {
	String name = "娜娜";
	// 外部类的方法
	public void test() {
		// 4.如果局部内部类要访问外部局部变量,那么需要用final来修饰该外部局部变量。
		final int num = 18; // JDK1.8之后可以省略final
		// 局部内部类
		class InnerClass {
			// 局部内部类的成员变量
			public String name;
		    // 局部内部类的构造方法
			public InnerClass() {} // 无参构造方法
			public InnerClass(String name) { // 有参构造方法
				this.name = name;
			}
			// 局部内部类的成员方法
			public void show(String name) {
				// 3.局部变量,局部内部类成员变量,外部类成员变量同名时的访问
				System.out.println("局部变量:" + name);
				System.out.println("成员内部类成员变量:" + this.name);
				System.out.println("外部类成员变量:" + OuterClass.this.name);
				// 访问外部局部变量
				System.out.println("外部局部变量:" + num);
			}
			// 2、成员内部类中不能存在任何的静态变量和静态方法。
			// static String company = "北京尚学堂"; 编译错误
			// public static test() {} 编译错误
		}
		
		// 1、局部内部类只能在对应的局部作用域中实例化。
		InnerClass in = new InnerClass("小明");
		// 获取局部内部类中的成员变量
		System.out.println("name:" + in.name);
		// 调用局部内部类中的成员方法
		in.show("小花");
	}
}
// 测试类
public class InnerClassDemo2 {
	public static void main(String[] args) {
		new OuterClass().test();
	}
}

4. anonymous inner classes

Anonymous inner classes is no internal class name, it is a local inner class.

Provided that the use of anonymous inner classes must inherit a parent class or implement an interface, it is to create a shortcut to a type of sub-class object.

  • Definition Format
class 外部类 {
	public 返回值类型 方法名(参数列表) {
		new 接口() | 父类构造器(参数列表) {
			// 其他代码
		}
	}
}

[Example] does not use anonymous inner classes Case

// 父类或一个接口,用于局部内部类继承或实现
abstract class Parent {
	abstract void show1();
	abstract void show2();
}
// 外部类
class OuterClass {
	public void test() {
		// 局部内部类
		class InnerClass extends Parent {
			@Override
			void show1() {
				System.out.println("内部类中的show1方法");
			}
			@Override
			void show2() {
				System.out.println("内部类中的show2方法");
			}
		}
		// 局部内部类实例化。
		InnerClass in = new InnerClass();
		in.show1();
		in.show2();
	}
}

Use of anonymous objects embodiment, the subclass defined (including inherited parent class or implement interfaces) and create a subclass object two primary steps performed by a format. Although two steps, but both steps are accomplished together.

[Example] anonymous inner classes use cases

// 父类或一个接口,用于匿名内部类继承或实现
abstract class Parent {
	abstract void show1();
	abstract void show2();
}
// 外部类
class OuterClass {
	public void test() {
		// 匿名内部类
		Parent p = new Parent() {
			@Override
			void show1() {
				System.out.println("匿名内部类中的show1方法");
			}
			@Override
			void show2() {
				System.out.println("匿名内部类中的show2方法");
			}
		};
		p.show1();
		p.show2();
	}
}

Anonymous inner classes if you do not define a variable reference, also anonymous object.

  • Anonymous inner classes Precautions
  1. Anonymous inner classes as a local inner class, so all the local internal restrictions take effect on the same kind of anonymous inner classes.

  2. Anonymous inner classes is not defined in the constructor, the completion of initialization by the configuration block.

  3. Anonymous inner classes carefully add unique member variables and member methods, as inconvenient to use!

The static inner classes

Keywords can be modified static member variables, methods, code blocks, static inner classes can also be modified using a modified static inner class we call static inner classes.

[Example] static inner classes Case

// 外部类
class OuterClass {
	// 静态内部类,用static来修饰
	public static class InnerClass {
		// 静态内部类的成员变量
		String name;
	    // 静态内部类的构造方法
		public InnerClass() {} // 无参构造方法
		public InnerClass(String name) { // 有参构造方法
			this.name = name;
		}
		// 静态内部类的成员方法
		public void show() {
			System.out.println("静态内部类的show方法");
		}
	}
	// 外部类静态方法
	public static void test() {
		// 初始化静态内部类
		InnerClass inner = new InnerClass("小明");
		// 获取静态内部类中的成员变量
		System.out.println("name:" + inner.name);
		// 调用静态内部类中的成员方法
		inner.show();
	}
}
  • Static class access methods

. External static inner class name class name variable name = new class name external static inner class name (parameter list);

[Example] static class access code demonstrates the way

// 外部类
class OuterClass {
	// 静态内部类,用static来修饰
	public static class InnerClass {
		// 静态内部类的成员变量
		String name;
	    // 静态内部类的构造方法
		public InnerClass() {} // 无参构造方法
		public InnerClass(String name) { // 有参构造方法
			this.name = name;
		}
		// 静态内部类的成员方法
		public void show() {
			System.out.println("静态内部类的show方法");
		}
	}
}
// 测试类
public class InnerClassDemo4 {
	public static void main(String[] args) {
		// 创建静态内部类
		OuterClass.InnerClass inner = new OuterClass.InnerClass("小明");
		// 获取静态内部类中的成员变量
		System.out.println("name:" + inner.name);
		// 调用静态内部类中的成员方法
		inner.show();
	}
}
  • Static inner classes Precautions
  1. Create static inner classes do not need to rely on external classes.

  2. Static inner classes can not use a non-static member variables and methods of the outer class.

  3. If the internal class defines a static member, then that inner classes must also be static.

  • Call static inner classes is static variables and static methods
  1. Call static variables:OuterClass.InnerClass.staticVariable

  2. Call the static method:OuterClass.InnerClass.staticFunction();

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 785

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104619480