亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java 嵌套静态类

Java 嵌套静态类

在本教程中,您将借助示例了解嵌套静态类。您还将了解静态类与内部类有何不同。

正如在前面的教程中学到的,我们可以在 Java 中的另一个类中包含一个类。这样的类称为嵌套类。在 Java 中,嵌套类有两种类型:

我们已经在之前的教程中讨论过内部类。如果您想了解内部类,请访问 Java 嵌套类。

在本教程中,我们将学习嵌套静态类。


Java 嵌套静态类

我们使用关键字static 使我们的嵌套类静态化。

注意: 在 Java 中,只允许嵌套类是静态的。

与常规类一样,静态嵌套类可以包括静态和非静态字段和方法。例如,

Class Animal {
   static class Mammal {
      // static and non-static members of Mammal
   }
   // members of Animal
} 

静态嵌套类与外部类相关联。

要访问静态嵌套类,我们不需要外部类的对象。


示例:静态嵌套类

class Animal {

// inner class
   class Reptile {
      public void displayInfo() {
        System.out.println("I am a reptile.");
      }
   }

// static class
   static class Mammal {
      public void displayInfo() {
        System.out.println("I am a mammal.");
      }
   }
}

class Main {
   public static void main(String[] args) {
      // object creation of the outer class
      Animal animal = new Animal();

      // object creation of the non-static class
      Animal.Reptile reptile = animal.new Reptile();
      reptile.displayInfo();

      // object creation of the static nested class
      Animal.Mammal mammal = new Animal.Mammal();
      mammal.displayInfo();

   }
}

输出

I am a reptile.
I am a mammal.

在上面的程序中,我们有两个嵌套类 Mammal爬行动物 在类 Animal 中 .

为了创建非静态类 Reptile 的对象,我们使用了

Animal.Reptile reptile = animal.new Reptile()

创建静态类 Mammal 的对象 ,我们用过

Animal.Mammal mammal = new Animal.Mammal()

访问外部类的成员

在 Java 中,静态嵌套类与外部类相关联。这就是为什么静态嵌套类只能访问外部类的类成员(静态字段和方法)。

让我们看看如果我们尝试访问外部类的非静态字段和方法会发生什么。

示例:访问非静态成员

class Animal {
  static class Mammal {
   public void displayInfo() {
     System.out.println("I am a mammal.");
   }
 }

 class Reptile {
   public void displayInfo() {
     System.out.println("I am a reptile.");
   }
 }

 public void eat() {
   System.out.println("I eat food.");
 }
}

class Main {
 public static void main(String[] args) {
   Animal animal = new Animal();
   Animal.Reptile reptile = animal.new Reptile();
   reptile.displayInfo();

   Animal.Mammal mammal = new Animal.Mammal();
   mammal.displayInfo();
   mammal.eat();
 }
}

输出

Main.java:28: error: cannot find symbol
    mammal.eat();
          ^
  symbol:   method eat()
  location: variable mammal of type Mammal
1 error
compiler exit status 1

在上面的例子中,我们创建了一个非静态方法 eat() 在类 Animal 中 .

现在,如果我们尝试访问 eat() 使用对象 mammal ,编译器报错。

这是因为哺乳动物 是静态类的对象,我们不能从静态类中访问非静态方法。


静态顶级类

如上所述,只有嵌套类可以是静态的。我们不能有静态的顶级类。

让我们看看如果我们尝试将顶级类设为静态会发生什么。

static class Animal {
 public static void displayInfo() {
   System.out.println("I am an animal");
 }
}

class Main {
 public static void main(String[] args) {
   Animal.displayInfo();
 }
}

输出

Main.java:1: error: modifier static not allowed here
static class Animal {
       ^
1 error
compiler exit status 1

在上面的例子中,我们尝试创建一个静态类 Animal .由于Java不允许静态顶级类,我们会得到一个错误。


java

  1. C# 静态关键字
  2. C# 嵌套类
  3. Java final 关键字
  4. Java instanceof 运算符
  5. Java 继承
  6. Java 嵌套和内部类
  7. Java 匿名类
  8. Java 单例类
  9. Java 反射
  10. Java ObjectOutputStream 类
  11. Java 泛型
  12. Java 文件类