首页  >  文章  >  Java  >  Java 中的嵌套类

Java 中的嵌套类

WBOY
WBOY原创
2024-08-30 16:00:19560浏览

嵌套类是指位于另一个类内部的类。 Java允许我们在java中创建嵌套类。嵌套类是其外部类的成员之一。它还可以声明为公共、私有、受保护或默认。嵌套类可以访问外部类的其他成员,反之则不可能。这意味着外部类无法访问嵌套类成员,因为嵌套类是其封闭外部类的成员,因此点 (.) 用于访问嵌套类及其成员。嵌套类没有任何静态关键字。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java 中嵌套类的类别

嵌套类分为两类:

  1. 静态嵌套类: 我们将使用关键字“static”声明的嵌套类称为静态嵌套类。静态嵌套类可以通过引用外部类来访问。嵌套类具有静态类,无法访问外部类的非静态变量和方法。
  2. 内部类:非静态嵌套类是未静态声明的内部类。 即使声明为私有,静态嵌套类也无法访问所有静态和非静态变量和方法。内部类也有两种类型。
  • 本地内部类
  • 匿名内部类

语法:

在下面的语法中,OuterClass 有一个内部类 InnerClass,称为嵌套类。

//Outer class
class OuterClass {
//Inner class as a nested class
class InnerClass {
....
}
}

Java 中嵌套类的使用

在编程世界中,Nested 类扮演着重要的角色,如下所示:

  • 类的嵌套就像将类分组到另一个类范围中。您不能在其他地方使用嵌套类。
  • 带来可读性和可维护性:类的嵌套提供了更好的代码可读性,并且更容易维护,因为它不会影响其他类。
  • 减少代码:使用嵌套类可以减少代码行数,从而实现优化。
  • 增加封装性: 在嵌套类中,内部类可以访问其封闭外部类的成员。同时,外部类不能直接访问内部类成员。此外,内部类对外部程序不可见或不可访问。

Java 中嵌套类的示例

下面给出了 Java 中嵌套类的示例:

示例#1

在这个例子中,你可以通过引用外部类来观察内部类的实例化。

代码:

// Outer class which contains nested class
class Engine{
static double fuel = 20.0;
//static nested class
static class Ignition{
void showFuelSpend() {
System.out.println("Fuel Spend = " + fuel + " Ltrs");
}
}
}
//class uses nested class inside it
public class NestedClassExample{
public static void main(String[] args) {
//creating object of the nested class by referencing parent class
Engine.Ignition engIgnitObj = new Engine.Ignition();
//calling method of the nested class
engIgnitObj.showFuelSpend();
}
<u>}</u>

输出:

Java 中的嵌套类

示例#2

在这个例子中,我们可以看到内部类是如何实例化的。您将需要内部类的实例来创建内部类的实例。创建外部类的实例后,就可以在其中创建嵌套类的实例。

代码:

//Outer class
class Volume{
double x, y, z;
Volume(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
// Nested inner class
class Measurement{
//method to calculate the total area
void totalArea(double i, double j) {
System.out.println("\nArea for the provided container is " + (i * j));
}
//method to calculate the total volume
void totalVolume(double i, double j, double k) {
System.out.println("\nVolume for the provided container is " + (i * j * k));
}
}
}
public class NestedClassCalcExample {
public static void main(String[] args) {
//passing here all the parameter to constructor
Volume volObj = new Volume(30.0, 25, 18.0);
Volume.Measurement volMeasureObj = volObj.new Measurement();
// calculating total area
volMeasureObj.totalArea(volObj.x, volObj.y);
// calculating total volume
volMeasureObj.totalVolume(volObj.x, volObj.y, volObj.z);
}
}

输出:

Java 中的嵌套类

示例 #3

此示例演示了如何在外部类的实例中实例化嵌套类对象。

代码:

//outer class
class Electronic{
String circuitName,
String circuitType;
double circuitCost;
//constructor of outer class
Electronic(String name, String type, double cost) {
this.circuitName = name;
this.circuitType = type;
this.circuitCost = cost;
}
String getCircuitName() {
return this.circuitName;
}
//nested class
class Circuit{
String circuitType;
double circuitCost;
void setCircuitType() {
this.circuitType = "Transistor";
this.circuitCost = 430.0;
}
//get circuit type using this method
String getCircuitType(){
return this.circuitType;
}
//get circuit cost using this method
double getCircuitCost(){
return this.circuitCost;
}
}
}
public class Product{
public static void main(String[] args) {
Electronic elObj = new Electronic("Amplifier", "Integrated", 375.0);
Electronic.Circuit circuit = elObj.new Circuit();
//printing here the values before reset it
System.out.println("\nCircuit Name : " + elObj.circuitName);
System.out.println("\nCircuit Type : " + elObj.circuitType);
System.out.println("\nCircuit Cost : " + elObj.circuitCost);
//resetting some value
circuit.setCircuitType();
//printing here the values before reset it
System.out.println("\n\nCircuit Name : " + elObj.getCircuitName());
System.out.println("\nCircuit Type : " + circuit.getCircuitType());
System.out.println("\nCircuit Cost : " + circuit.getCircuitCost());
}
}

输出:

Java 中的嵌套类

结论

在上面的文章中,我们回顾了 Nested 类在 java 中的重要性。利用类的嵌套是一个很好的做法。上面的文章还介绍了Nested类的可用性。

以上是Java 中的嵌套类的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Wrapper Class in Java下一篇:Java Matcher