Java inner class members of the inner class static inner class local inner classes Anonymous inner classes

Foreword

Java, there are some classes exist inside other classes, there are some classes that exist inside other classes. The so-called internal class that is defined in class category, the category is contained inside the class outer class .
And class variables or objects similar to that of the interior is divided into categories depending on the location and effectiveness: internal class members, local inner classes, anonymous inner classes and static inner classes
Let's sum up:


Members of the inner class

As a class member inner class appears, its scope and member variables are very similar.
The first to write a class with an inner class members:

class OuterClass
{
	private int num = 0;
	public void f()
	{
		System.out.print("Hello World!!");
	}
	
	class InnerClass
	{
		public void doSomething()
		{
			f();
			System.out.println(num);
		}
	}
}

Methods and variables and outside the class

In the outer class, both variables and methods from each other are used , the internal members of classes and methods can be accessed outside the class, the external class through internal class object to access the internal members of the classes and methods. Even members of the methods are private or modified.

Outside the outer class , through an internal class object is not accessible to the private inner class, and this class with the outside.

Well, since you can call each other, we will face the problem of naming conflicts, where naming conflicts contains the inner class local variables , internal class member variables , external class member variables conflict between. To solve this problem, mechanisms and solutions to solve java class method adopted by the local variables and member variables of conflict: Use this keyword class and external calls.
Specifically, in the method within the class, if naming conflicts (some cases very short, assuming there is a conflict of the three variables), we can:

  • Direct use of variable names, access local variables inside the class
  • Use this keyword to access internal class member variables
  • Use an external class name .This , access to external class member variables
    can refer to the following code:
class Outer
{
	private int x;
	class Inner
	{
		private int x;
		public void doSomething(int x)
		{
			x = 1;
			this.x = 2;
			Outer.this.x = 3;
		}
	}
}

Examples inner class

Outside the outer class:

Inside the object class must rely on the object outside the class! !
Inside the object class must rely on the object outside the class! !
Inside the object class must rely on the object outside the class! !

(The important thing to say three times)

That is, if there is no external object that already exists, it is impossible to target its members within the class appear.

Examples inner class object syntax is:

外部类.内部类 对象名 = 外部类对象.new 内部类();

Code written:

OuterClass out = new OuterClass();
OuterClass.InnerClass in = out.new InnerClass();

Examples of the first statement and the external object, and then the new keyword in the object instance of the class object inside the outer class.


Outside the class, you can create an external class object directly.
Thus, the method can also provide a class object is created in the interior of the outer class, thereby obtaining internal class object by calling this method.
For example, this method was added in the outer class:

public InnerClass getInner()
{
	return new InnerClass();
}

Then call this method to instantiate inner class:

OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.getInner();

However, it should be noted that this definition of the way, still require the presence of an object outside the class to call the method.

what? You said that method would not write a static class can be invoked by it?
Answer this question, only to note the contents of the class is loaded, although loaded with static method, but it is not a member of the loading content within the class, so check in to this method, when the interpreter does not recognize it and will prompt an error. Do not believe you can see:
Here Insert Picture Description
error message:
Here Insert Picture Description
What? The static inner classes have not changed enough yet? emm, you are right, but it is not a member of the inner class ah / doge. This is not a member of the inner class static inner classes will be elaborated in the last part.

Finally, to insert a mouth, a member of the same inner classes and member variables also can specify the scope , which determines where you can access to it. If the modification is private, it may only be obtained outside of class for internal use or method by means of an external object of the class, and also it should be noted, private inner class is not limited to access by external class name to the class name outside the outer class : similar to this:
Here Insert Picture Description
through external access to internal class category being given.
Here Insert Picture Description


Partial inner class

With members of the bedding inside the column, local inner class becomes easier to understand.

First, define the internal local class defined outside the class or method is applied to any class defined in the interior can be roughly understood as the inner class moved to the outer class class method, so that it can from outside the class scope It becomes a member within the local method, so named local inner classes.

In use, the need to pay attention to is like what you just said. This class is part of the method, not part of the outer class .
In addition, it should be noted that the local inner class can access only final modification methods are constants, and its value can not be modified .

If it is written like this:

class Outer
{
	public void doSomethingOut(final int x,int y)
	{
		class Inner
		{
			private int a = x;
			private int b = y;
		}
	}
}

Y visit will be incorrect report:
Here Insert Picture Description


Anonymous inner classes

接着来介绍一个比较特殊的类,这个类有些惨,他们总是被一次性使用,也因此没有自己的类名,成全了别的类,自己却不留功与名。
那么这个类从那里来的呢,还得从接口实例化对象说起。众所周知,接口是一种抽象类,而一个抽象类是无法实例化自身类的对象的,除非下转型为子类的对象。这个性质对于接口来讲,想要给一个接口类型的变量实例化对象,就必须实例化一个实现了该接口的类
有时候,实例化一个接口对象只是希望这个对象能够纯粹的实现接口中的方法,可以选择写一个类来实现这个接口并重写这些方法,但又不太需要一直保留这个类,毕竟它占着一个命名。
所以java提供了匿名内部类这种类,可以在实现接口时临时为接口编写一次性的实现接口的类,而由于是一次性的使用,这样的类没有命名,于是成为匿名内部类。

具体的语法如下:

接口名 变量 = new 接口(){
	匿名内部类内容
};

需要注意的有两点:

  • 接口变量引用的,是匿名内部类的对象而非接口的对象
  • 在实现内部类后,不要忘记写分号
    举个例子:
interface A
{
	public void doSomething();
}
public class Main {
	public static void main(String[] args)
	{
		A a = new A(){
			public void doSomething()
			{
				System.out.print("hello world");
			}
		};
	}
}

当然,也可以选择在继承了某个接口的类中实现一个通过匿名内部类获得接口对象的方法。
例如:

class aClass implements Comparator<Integer>
{
	public int compare(Integer o1, Integer o2) {
		return o1 - o2;
	}
	static public Comparator<Integer> getComparator()
	{
		return new Comparator<Integer>(){
			public int compare(Integer o1,Integer o2)
			{
				return o1 - o2;
			}
		};
	}
}
public class Main {
	public static void main(String[] args)
	{
		Comparator<Integer> judge = aClass.getComparator();
	}
}

静态内部类

最后,让我们来分析一下这个在一开始就提到的内部类。

在内部类前面添加修饰符static,我们就得到了一个静态内部类。

由于它是静态的,导致了它在表现出的性质上与其他内部类存在不同,不同之处有:

  • 静态内部类可以声明静态成员,而非静态内部类不可以
  • 静态内部类不可以访问外部类的非静态成员
  • 创建静态内部类对象不需要外部类对象
    可参考下图:
    Here Insert Picture Description
Published 17 original articles · won praise 2 · Views 459

Guess you like

Origin blog.csdn.net/wayne_lee_lwc/article/details/104626386