📜  Java中的嵌套类

📅  最后修改于: 2022-05-13 01:54:30.430000             🧑  作者: Mango

Java中的嵌套类

在Java中,可以在另一个类中定义一个类,这样的类称为嵌套类。它们使您能够对仅在一个地方使用的类进行逻辑分组,从而增加了封装的使用,并创建了更具可读性和可维护性的代码。

  • 嵌套类的范围受其封闭类的范围限制。因此,在下面的示例中,类NestedClass不独立于类OuterClass存在。
  • 嵌套类可以访问它所嵌套的类的成员,包括私有成员。反之亦然,即封闭类可以访问嵌套类的成员。
  • 嵌套类也是其封闭类的成员。
  • 作为其封闭类的成员,嵌套类可以声明为privatepublicprotected包私有(默认)。
  • 嵌套类分为两类:
    1. 静态嵌套类:声明为静态的嵌套类称为静态嵌套类。
    2. 内部类:内部类是非静态嵌套类。

句法:

class OuterClass
{
...
    class NestedClass
    {
        ...
    }
}

d

静态嵌套类

在普通或常规内部类的情况下,没有外部类对象存在,就不可能有内部类对象。即,内部类的对象总是与外部类对象强关联。但是在静态嵌套类的情况下,如果没有外部类对象存在,则可能存在静态嵌套类对象。即,静态嵌套类的对象与外部类对象没有强关联。
与类方法和变量一样,静态嵌套类与其外部类相关联。和静态类方法一样,静态嵌套类不能直接引用在其封闭类中定义的实例变量或方法:它只能通过对象引用来使用它们。
使用封闭的类名访问它们。

OuterClass.StaticNestedClass

例如,要为静态嵌套类创建对象,请使用以下语法:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();
// Java program to demonstrate accessing
// a static nested class
 
// outer class
class OuterClass
{
    // static member
    static int outer_x = 10;
     
    // instance(non-static) member
    int outer_y = 20;
     
    // private member
    private static int outer_private = 30;
     
    // static nested class
    static class StaticNestedClass
    {
        void display()
        {
            // can access static member of outer class
            System.out.println("outer_x = " + outer_x);
             
            // can access display private static member of outer class
            System.out.println("outer_private = " + outer_private);
             
            // The following statement will give compilation error
            // as static nested class cannot directly access non-static members
            // System.out.println("outer_y = " + outer_y);
         
        }
    }
}
 
// Driver class
public class StaticNestedClassDemo
{
    public static void main(String[] args)
    {
        // accessing a static nested class
        OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
         
        nestedObject.display();
         
    }
}

输出:

outer_x = 10
outer_private = 30

内部类

要实例化内部类,您必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

有两种特殊的内部类:

  1. 本地内部类
  2. 匿名内部类
// Java program to demonstrate accessing
// a inner class
 
// outer class
class OuterClass
{
    // static member
    static int outer_x = 10;
     
    // instance(non-static) member
    int outer_y = 20;
     
    // private member
    private int outer_private = 30;
     
    // inner class
    class InnerClass
    {
        void display()
        {
            // can access static member of outer class
            System.out.println("outer_x = " + outer_x);
             
            // can also access non-static member of outer class
            System.out.println("outer_y = " + outer_y);
             
            // can also access a private member of the outer class
            System.out.println("outer_private = " + outer_private);
         
        }
    }
}
 
// Driver class
public class InnerClassDemo
{
    public static void main(String[] args)
    {
        // accessing an inner class
        OuterClass outerObject = new OuterClass();
        OuterClass.InnerClass innerObject = outerObject.new InnerClass();
         
        innerObject.display();
         
    }
}

输出:

outer_x = 10
outer_y = 20
outer_private = 30

普通或常规类与静态嵌套类的比较

S.NONormal/Regular inner classStatic nested class
1.Without an outer class object existing, there cannot be an inner class object. That is, the inner class object is always associated with the outer class object.Without an outer class object existing, there may be a static nested class object. That is, static nested class object is not associated with the outer class object.
2.Inside normal/regular inner class, static members can’t be declared.Inside static nested class, static members can be declared.
3.As main() method can’t be declared, regular inner class can’t be invoked directly from the command prompt.As main() method can be declared, the static nested class can be invoked directly from the command prompt.
4.Both static and non static members of outer class can be accessed directly.Only a static member of outer class can be accessed directly.