原文: https://www.programiz.com/java-programming/nested-inner-class
在本教程中,您将借助示例学习 Java 中的嵌套类及其类型。
在 Java 中,您可以在另一个类中定义一个类。 这种类称为nested class。 例如,
class OuterClass {// ...class NestedClass {// ...}}
您可以使用 Java 创建两种类型的嵌套类。
- 非静态嵌套类(内部类)
- 静态嵌套类
推荐阅读:
首先让我们看一下非静态嵌套类。
非静态嵌套类(内部类)
非静态嵌套类是另一个类中的一个类。 它有权访问封闭类(外部类)的成员。 通常称为inner class。
由于inner class存在于外部类中,因此您必须首先实例化外部类,以便实例化内部类。
这是一个如何在 Java 中声明内部类的示例。
示例 1:内部类
class CPU {double price;// nested classclass Processor{// members of nested classdouble cores;String manufacturer;double getCache(){return 4.3;}}// nested protected classprotected class RAM{// members of protected nested classdouble memory;String manufacturer;double getClockSpeed(){return 5.5;}}}public class Main {public static void main(String[] args) {// create object of Outer class CPUCPU cpu = new CPU();// create an object of inner class Processor using outer classCPU.Processor processor = cpu.new Processor();// create an object of inner class RAM using outer class CPUCPU.RAM ram = cpu.new RAM();System.out.println("Processor Cache = " + processor.getCache());System.out.println("Ram Clock speed = " + ram.getClockSpeed());}}
输出:
Processor Cache = 4.3Ram Clock speed = 5.5
在上面的程序中,有两个嵌套类:CPU和RAM位于外部类内部:CPU。 我们可以将内部类声明为受保护的。 因此,我们已将 RAM 类声明为受保护的。
在Main类里面
我们首先创建了一个名为
cpu的外部类CPU的实例。然后使用外部类的实例创建内部类的对象: ```java CPU.Processor processor = cpu.new Processor;
CPU.RAM ram = cpu.new RAM();
**注意**:我们使用点(`.`)运算符使用外部类创建内部类的实例。---<a name="157bb9c6"></a>### 访问内部类中的外部类成员通过使用`this`关键字,我们可以访问外部类的成员。 如果您想了解`this`关键字,请访问 [Java `this`关键字](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html)。<a name="286610f5"></a>### 示例 2:访问成员```javaclass Car {String carName;String carType;// assign values using constructorpublic Car(String name, String type) {this.carName = name;this.carType = type;}// private methodprivate String getCarName() {return this.carName;}// inner classclass Engine {String engineType;void setEngine() {// Accessing the carType property of Carif(Car.this.carType.equals("4WD")){// Invoking method getCarName() of Carif(Car.this.getCarName().equals("Crysler")) {this.engineType = "Smaller";} else {this.engineType = "Bigger";}}else{this.engineType = "Bigger";}}String getEngineType(){return this.engineType;}}}public class Main {public static void main(String[] args) {// create an object of the outer class CarCar car1 = new Car("Mazda", "8WD");// create an object of inner class using the outer classCar.Engine engine = car1.new Engine();engine.setEngine();System.out.println("Engine Type for 8WD= " + engine.getEngineType());Car car2 = new Car("Crysler", "4WD");Car.Engine c2engine = car2.new Engine();c2engine.setEngine();System.out.println("Engine Type for 4WD = " + c2engine.getEngineType());}}
输出:
Engine Type for 8WD= BiggerEngine Type for 4WD = Smaller
在上面的程序中,我们在外部类Car内有一个名为Engine的内部类。 在这里,请注意这行,
if(Car.this.carType.equals("4WD")) {...}
我们正在使用this关键字来访问外部类的carType变量。 您可能已经注意到,我们使用Car.this.carType代替了this.carType。
这是因为,如果我们没有提到外部类Car的名称,那么this关键字将代表内部类内部的成员。
同样,我们也从内部类访问外部类的方法。
if (Car.this.getCarName().equals("Crysler") {...}
重要的是要注意,尽管getCarName()是private方法,但我们仍可以从内部类访问它。
静态嵌套类
在 Java 中,我们还可以在另一个类中定义一个static类。 此类称为static nested class。 静态嵌套类不称为静态内部类。
与内部类不同,静态嵌套类无法访问外部类的成员变量。 这是因为静态嵌套类不需要您创建外部类的实例。
OuterClass.NestedClass obj = new OuterClass.NestedClass();
在这里,我们仅通过使用外部类的类名来创建静态嵌套类的对象。 因此,无法使用OuterClass.this引用外部类。
示例 3:静态内部类
class MotherBoard {// static nested classstatic class USB{int usb2 = 2;int usb3 = 1;int getTotalPorts(){return usb2 + usb3;}}}public class Main {public static void main(String[] args) {// create an object of the static nested class// using the name of the outer classMotherBoard.USB usb = new MotherBoard.USB();System.out.println("Total Ports = " + usb.getTotalPorts());}}
输出:
Total Ports = 3
在上面的程序中,我们在类MotherBoard中创建了一个名为USB的静态类。 注意这一行,
MotherBoard.USB usb = new MotherBoard.USB();
在这里,我们使用外部类的名称创建USB的对象。
现在,让我们看看如果尝试访问外部类的成员会发生什么:
示例 4:在静态内部类内部访问外部类的成员
class MotherBoard {String model;public MotherBoard(String model) {this.model = model;}// static nested classstatic class USB{int usb2 = 2;int usb3 = 1;int getTotalPorts(){// accessing the variable model of the outer classsif(MotherBoard.this.model.equals("MSI")) {return 4;}else {return usb2 + usb3;}}}}public class Main {public static void main(String[] args) {// create an object of the static nested classMotherBoard.USB usb = new MotherBoard.USB();System.out.println("Total Ports = " + usb.getTotalPorts());}}
当我们尝试运行该程序时,将出现错误:
error: non-static variable this cannot be referenced from a static context
这是因为我们没有使用外部类的对象来创建内部类的对象。 因此,没有引用存储在Motherboard.this中的外部类Motherboard。
要记住的要点
- Java 将内部类视为类的常规成员。 它们就像在类中声明的方法和变量一样。
- 由于内部类是外部类的成员,因此您可以将
private,protected之类的任何访问修饰符应用于内部类,这在普通类中是不可能的。 - 由于嵌套类是其封闭的外部类的成员,因此可以使用点(
.)表示法来访问嵌套类及其成员。 - 使用嵌套的类将使您的代码更具可读性,并提供更好的封装。
- 非静态嵌套类(内部类)可以访问外部/封闭类的其他成员,即使它们被声明为私有的也是如此。
