Java 简明教程
Java - Inner classes
Java Inner Class
Java inner class 是 class ,一个在另一个类中定义的类。内部类的概念配合使用嵌套 Java 类,其中使用外部类和内部类。定义内部类的主类称为外部类,外部类中的所有其他类被称为 Java inner classes 。
A Java inner class is a class that is defined inside another class. The concept of inner class works with nested Java classes where outer and inner classes are used. The main class in which inner classes are defined is known as the outer class and all other classes which are inside the outer class are known as Java inner classes.
Nested Classes
在 Java 中,与 methods 一样, variables of a class 也可以具有另一个类作为其成员。允许在 Java 中编写一个类到另一个类中。编写在其中的类称为 nested class ,容纳内部类的类称为 outer class 。
In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.
Syntax
以下是要写一个嵌套类的语法。这里,类 Outer_Demo 是外部类,类 Inner_Demo 是嵌套类。
Following is the syntax to write a nested class. Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.
class Outer_Demo {
class Inner_Demo {
}
}
嵌套类分为两种 −
Nested classes are divided into two types −
-
Non-static nested classes − These are the non-static members of a class.
-
Static nested classes − These are the static members of a class.
Inner Classes (Non-static Nested Classes)
内部类是 Java 中的安全机制。我们知道一个类不能与 access modifier private 关联,但如果我们将类作为其他类的成员,则内部类可以被设为私有的。这还用于访问一个类的私有成员。
Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.
Types of Java Inner Classes
根据定义它们的位置和方式,内部类有三种类型。它们是 −
Inner classes are of three types depending on how and where you define them. They are −
-
Inner Class
-
Method-local Inner Class
-
Anonymous Inner Class
Creating an Inner Class
创建内部类很简单。你只需要在一个类中写一个类。与类不同,内部类可以是私有的,一旦你将内部类声明为私有的,则无法通过该类外部的对象访问。
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.
以下是创建内部类并访问它的程序。在给定的示例中,我们将内部类设为私有,并通过一个方法来访问该类。
Following is the program to create an inner class and access it. In the given example, we make the inner class private and access the class through a method.
Example: Creating an inner class in Java
class Outer_Demo {
int num;
// inner class
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}
// Accessing he inner class from the method within
void display_Inner() {
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
public class My_class {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Accessing the display_Inner() method.
outer.display_Inner();
}
}
在这里你可以看到 Outer_Demo 是外部类,Inner_Demo 是内部类,我们在这个方法中实例化内部类,并且从 main 方法调用此方法。
Here you can observe that Outer_Demo is the outer class, Inner_Demo is the inner class, display_Inner() is the method inside which we are instantiating the inner class, and this method is invoked from the main method.
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
This is an inner class.
Accessing the Private Members
如前所述,内部类还用来访问类的私有成员。假设一个类有要访问它们的私有成员。在其中写一个内部类,从内部类中的一个方法(例如 getValue())中返回私有成员,最后从另一个类(你要从中访问私有成员的类)调用内部类的 getValue() 方法。
As mentioned earlier, inner classes are also used to access the private members of a class. Suppose, a class is having private members to access them. Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.
要实例化内部类,首先必须实例化外部类。此后,使用外部类的对象,以下是实例化内部类的方法。
To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the object of the outer class, following is the way in which you can instantiate the inner class.
Outer_Demo outer = new Outer_Demo();
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
以下程序显示如何使用内部类访问类的私有成员。
The following program shows how to access the private members of a class using inner class.
Example: Accessing the Private Members Using Inner Class
class Outer_Demo {
// private variable of the outer class
private int num = 175;
// inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}
public class My_class2 {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Instantiating the inner class
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
System.out.println(inner.getNum());
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
This is the getnum method of the inner class: 175
Method-local Inner Class
在 Java 中,我们可以在一个方法中写一个类,它将成为一个局部类型。与局部变量一样,内部类的范围限制在方法内。
In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method.
方法局部内部类只能在定义内部类的方法中实例化。以下程序演示如何使用方法局部内部类。
A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.
Example: Method-local Inner Class
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;
// method-local inner class
class MethodInner_Demo {
public void print() {
System.out.println("This is method inner class "+num);
}
} // end of inner class
// Accessing the inner class
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.my_Method();
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
This is method inner class 23
Anonymous Inner Class
未声明类名的内部类称为 anonymous inner class。对于匿名内部类,我们同时声明和实例化它们。通常,每当你需要重写类或接口的方法时,它们就会被使用。匿名内部类的语法如下 −
An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used whenever you need to override the method of a class or an interface. The syntax of an anonymous inner class is as follows −
Syntax: Anonymous Inner Class
AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
........
........
}
};
以下程序演示如何使用匿名内部类重写类的类。
The following program shows how to override the method of a class using anonymous inner class.
Example: Anonymous Inner Class
abstract class AnonymousInner {
public abstract void mymethod();
}
public class Outer_class {
public static void main(String args[]) {
AnonymousInner inner = new AnonymousInner() {
public void mymethod() {
System.out.println("This is an example of anonymous inner class");
}
};
inner.mymethod();
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
This is an example of anonymous inner class
同样,你也可以使用匿名内部类重写具体类和接口的方法。
In the same way, you can override the methods of the concrete class as well as the interface using an anonymous inner class.
Anonymous Inner Class as Argument
通常,如果一个方法接受接口、抽象类或具体类的对象,那么我们可以实现接口、扩展抽象类,并将对象传给方法。如果它是一个类,那么我们可以直接将它传给方法。
Generally, if a method accepts an object of an interface, an abstract class, or a concrete class, then we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, then we can directly pass it to the method.
但在所有这三种情况下,你都可以将匿名内部类传给方法。以下是将匿名内部类作为方法参数传递的语法 −
But in all the three cases, you can pass an anonymous inner class to the method. Here is the syntax of passing an anonymous inner class as a method argument −
obj.my_Method(new My_Class() {
public void Do() {
.....
.....
}
});
以下程序演示如何将匿名内部类作为方法参数传递。
The following program shows how to pass an anonymous inner class as a method argument.
Example
// interface
interface Message {
String greet();
}
public class My_class {
// method which accepts the object of interface Message
public void displayMessage(Message m) {
System.out.println(m.greet() +
", This is an example of anonymous inner class as an argument");
}
public static void main(String args[]) {
// Instantiating the class
My_class obj = new My_class();
// Passing an anonymous inner class as an argument
obj.displayMessage(new Message() {
public String greet() {
return "Hello";
}
});
}
}
如果您编译并执行上面的程序,您将获得以下结果-
If you compile and execute the above program, it gives you the following result −
Hello, This is an example of anonymous inner class as an argument
Static Nested Class
静态内部类是一个嵌套类,它是外部类的静态成员。可以使用其他静态成员在不实例化外部类的情况下访问它。与静态成员一样,静态嵌套类无权访问外部类的实例变量和方法。静态嵌套类的语法如下-
A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class. The syntax of static nested class is as follows −
Syntax
class MyOuter {
static class Nested_Demo {
}
}
实例化一个静态嵌套类与实例化一个内部类略有不同。以下程序演示了如何使用静态嵌套类。
Instantiating a static nested class is a bit different from instantiating an inner class. The following program shows how to use a static nested class.
Example
public class Outer {
static class Nested_Demo {
public void my_method() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
Outer.Nested_Demo nested = new Outer.Nested_Demo();
nested.my_method();
}
}
如果您编译并执行上述程序,将得到以下结果 −
If you compile and execute the above program, you will get the following result −
This is my nested class