Java-类-嵌套类

时间:2020-02-23 14:36:26  来源:igfitidea点击:

在本教程中,我们将学习Java编程语言中的嵌套类。

在先前的教程中,我们已经讨论了如何创建类以及如何实例化类的对象。
随时检查他们。

Java中的嵌套类是什么?

如果我们在另一个类中定义一个类,则此类称为嵌套类。

嵌套类的语法

class OuterClassName {

  //some code...
  
  class InnerClassName {
    //some code...
  }

}

其中," OuterClassName"是包含" InnerClassName"类的外部类的名称。

注意点!

以下是有关嵌套类的注意事项。

class A {
  class B {
    //some code...
  }
}
  • 如果在类A内定义了类B,则B不能独立于A而存在。

  • B类可以直接访问所有成员变量,包括A类的私有变量和方法。

  • 类A无法直接访问类B的成员变量和方法。

在下面的示例中,我们有一个外部类" Company"和一个内部类" Project"。

class Company {

  private int totalProject;

  Company() {
    System.out.println("Inside Company class constructor.");
    this.totalProject = 0;
    System.out.println("Total Company projects: " + this.totalProject);
  }

  public void createProject() {
    //creating a new project object
    Project prj1 = new Project(10);
    prj1.displayDuration();

    //creating another project object
    Project prj2 = new Project(20);
    prj2.displayDuration();

    //the following lines will give error
    //because outer class can't access the inner class
    //members directly
    //so, commented out
    //
    //System.out.println("DummyVariable of Project class : " + dummyVariable);
  }

  private void greetings() {
  	System.out.println("Greetings from private method of Company class.");
  }

  class Project {

    private int durationInDays;

    //don't try to access this directly from
    //outer class Company as it is a part of
    //the inner class Project
    public int dummyVariable = 10;

    Project(int duration) {
      System.out.println("Inside Project class constructor.");

      //set the project duration
      this.durationInDays = duration;

      //increment the total projects
      //note! totalProject is a private member of Company class
      //but it is accessible from the inner class Project
      totalProject++;
      System.out.println("Total Company projects: " + totalProject);

      //calling the private method of the outer class Company
      //from the inner class Project
      System.out.println("Calling private method of Company class from Project class.");
      greetings();
    }

    public void displayDuration() {
      System.out.println("Inside Project class displayDuration method.");
      System.out.println("Project duration: " + this.durationInDays);
    }

  }
}

public class Example {
  public static void main(String[] args) {
    //create an object
    Company obj = new Company();

    //now create a new project of the company
    obj.createProject();
  }
}
$javac Example.java 
$java Example
Inside Company class constructor.
Total Company projects: 0
Inside Project class constructor.
Total Company projects: 1
Calling private method of Company class from Project class.
Greetings from private method of Company class.
Inside Project class displayDuration method.
Project duration: 10
Inside Project class constructor.
Total Company projects: 2
Calling private method of Company class from Project class.
Greetings from private method of Company class.
Inside Project class displayDuration method.
Project duration: 20

在上面的代码中,我们创建一个外部类Company,在内部我们创建一个内部类Project

从输出中可以很容易地看出,我们可以直接从内部类Project中访问外部类Company的方法和变量。