Java 嵌套和内部类
Java 嵌套和内部类
在本教程中,您将通过示例了解 Java 中的嵌套类及其类型。
在 Java 中,您可以在另一个类中定义一个类。这样的类被称为 nested class
.例如,
class OuterClass {
// ...
class NestedClass {
// ...
}
}
您可以在 Java 中创建两种类型的嵌套类。
- 非静态嵌套类(内部类)
- 静态嵌套类
推荐阅读 :
- Java 访问修饰符
- Java 静态关键字
我们先来看看非静态嵌套类。
非静态嵌套类(内部类)
非静态嵌套类是另一个类中的一个类。它可以访问封闭类(外部类)的成员。俗称inner class
.
自inner class
存在于外部类中,必须先实例化外部类,才能实例化内部类。
这是一个如何在 Java 中声明内部类的示例。
示例 1:内部类
class CPU {
double price;
// nested class
class Processor{
// members of nested class
double cores;
String manufacturer;
double getCache(){
return 4.3;
}
}
// nested protected class
protected class RAM{
// members of protected nested class
double memory;
String manufacturer;
double getClockSpeed(){
return 5.5;
}
}
}
public class Main {
public static void main(String[] args) {
// create object of Outer class CPU
CPU cpu = new CPU();
// create an object of inner class Processor using outer class
CPU.Processor processor = cpu.new Processor();
// create an object of inner class RAM using outer class CPU
CPU.RAM ram = cpu.new RAM();
System.out.println("Processor Cache = " + processor.getCache());
System.out.println("Ram Clock speed = " + ram.getClockSpeed());
}
}
输出 :
Processor Cache = 4.3 Ram Clock speed = 5.5
在上面的程序中,有两个嵌套类:Processor 和 RAM 在外部类内部:CPU .我们可以将内部类声明为受保护的。因此,我们将 RAM 类声明为受保护。
在 Main 类中,
- 我们首先创建了一个外部类CPU的实例 命名为 cpu .
- 使用外部类的实例,然后我们创建内部类的对象:
CPU.Processor processor = cpu.new Processor; CPU.RAM ram = cpu.new RAM();
注意 :我们使用点 (.
) 运算符使用外部类创建内部类的实例。
在内部类中访问外部类的成员
我们可以使用 this 关键字访问外部类的成员。如果您想了解这个关键字,请访问 Java 的 this 关键字。
示例 2:访问成员
class Car {
String carName;
String carType;
// assign values using constructor
public Car(String name, String type) {
this.carName = name;
this.carType = type;
}
// private method
private String getCarName() {
return this.carName;
}
// inner class
class Engine {
String engineType;
void setEngine() {
// Accessing the carType property of Car
if(Car.this.carType.equals("4WD")){
// Invoking method getCarName() of Car
if(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 Car
Car car1 = new Car("Mazda", "8WD");
// create an object of inner class using the outer class
Car.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= Bigger Engine Type for 4WD = Smaller
在上面的程序中,我们有一个名为 Engine 的内部类 在外部类 Car 中 .在这里,注意这一行,
if(Car.this.carType.equals("4WD")) {...}
我们正在使用 this
访问 carType 的关键字 外部类的变量。您可能已经注意到,而不是使用 this.carType
我们使用了 Car.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 class
static 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 class
MotherBoard.USB usb = new MotherBoard.USB();
System.out.println("Total Ports = " + usb.getTotalPorts());
}
}
输出 :
Total Ports = 3
在上面的程序中,我们创建了一个名为 USB 的静态类 在类 MotherBoard 中 .注意这一行,
MotherBoard.USB usb = new MotherBoard.USB();
在这里,我们正在创建 USB 的对象 使用外部类的名称。
现在,让我们看看如果你尝试访问外部类的成员会发生什么:
示例 4:在静态内部类中访问外部类的成员
class MotherBoard {
String model;
public MotherBoard(String model) {
this.model = model;
}
// static nested class
static class USB{
int usb2 = 2;
int usb3 = 1;
int getTotalPorts(){
// accessing the variable model of the outer classs
if(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 class
MotherBoard.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
的引用 存储在 Motherboard.this
.
要记住的关键点
- Java 将内部类视为类的常规成员。它们就像在类中声明的方法和变量。
- 由于内部类是外部类的成员,您可以应用任何访问修饰符,例如
private
,protected
到您的内部班级,这在普通班级中是不可能的。 - 由于嵌套类是其封闭外部类的成员,您可以使用点 (
.
) 表示法来访问嵌套类及其成员。 - 使用嵌套类将使您的代码更具可读性并提供更好的封装。
- 非静态嵌套类(内部类)可以访问外部/封闭类的其他成员,即使它们被声明为私有。
java