📜  用于检查给定类是否为本地内部类的Java程序

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

用于检查给定类是否为本地内部类的Java程序

局部内部类是在块内声明的类。这些类仅在块内可见。因此,您需要在块内实例化该类。有时,这个块可以是 for 循环或 if-else 子句。这些类可以访问包含它的类的字段。本地内部类必须在定义它们的块中实例化。

我们将讨论使用以下方法如何检查给定的类是否是本地内部类。  

方法一:

  • 为了检查该类是否为本地内部类, Java.lang.Class 提供了一个名为isLocalClass()的方法。
  • 如果给定的类是本地类,则此方法返回 true,否则返回 false。请注意,“isLocalClass()”是在Java.lang.Class 类中定义的。因此,您需要先调用对象的“getClass()”方法。
  • 此方法返回表示对象类的 Class 实例。看下面的代码——
Java
// Java program to check if a class is inner
// local class using isLocalClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        class LocalInnerClass {
            // This is a local inner class
        }
        // Creating a LocalInnerClass object
        LocalInnerClass inner = new LocalInnerClass();
  
        // Getting the Class instance associated with
        // the class of inner(i.e. LocalInnerClass)
        // which is returned by getClass() method of inner
        Class localClass = inner.getClass();
  
        // isLocalClass() method of localClass returns true
        // if and only if localClass is a Local Class
        System.out.println(
            "Is inner a local class object? :"
            + localClass.isLocalClass());
    }
}


Java
// Java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        // Creating a GeeksForGeeks object
        // to create a GFG object
        GeeksForGeeks geeks = new GeeksForGeeks();
        // Creating a GFG object
        GFG gfg = geeks.new GFG();
  
        // getClass() returns a java.lang.Class
        // instance representing the GFG class
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
  
    class GFG {
        // This is a member class of GeeksForGeeks
    }
}


Java
// java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        GFG gfg = new GFG() {
            // This is an Anonymous class
            // that extends the GFG class
        }; // Remember to put the semicolon here
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
}


Java
// Java program to check if a class is a
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
        class Geeks {
            // This is an another local class
        }
  
        GFG gfg = new GFG();
        Geeks geeks = new Geeks();
  
        System.out.println(
            "Canonical Name of the class of gfg:"
            + gfg.getClass().getCanonicalName());
        System.out.println(
            "Canonical Name of the class of geeks:"
            + geeks.getClass().getCanonicalName());
    }
}


Java
// Java program to check if the class
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
  
        GFG gfg = new GFG();
  
        // Storing the class instance associated with gfg
        // i.e. Class instance representing GFG
        Class classInstance = gfg.getClass();
  
        // Checking whether the classInstance has a
        // canonical name
        boolean hasCanonicalName
            = (classInstance.getCanonicalName() != null);
  
        // If hasCanonicalName is false then it is sure that
        // either it is a local class or anonymous class or
        // an array whose component type does not have a
        // canonical name
  
        // Now check whether it is an anonymous Class
        boolean isAnonymousClass
            = classInstance.isAnonymousClass();
  
        // Check if it is an array
        boolean isArray = classInstance.isArray();
  
        // If all the three isArray,isAnonymousClass and
        // hasCanonicalName are false then the classInstance
        // is surely a local class
        boolean isLocalClass
            = (!hasCanonicalName && !isAnonymousClass
               && !isArray);
        System.out.println(
            "Is gfg a local class instance?: "
            + isLocalClass);
    }
}


输出
Is inner a local class object? :true

方法二:

  • 这种方法是关于检查类的规范名称,然后确定它是否是本地类。
  • Java.lang 中定义的getCanonicalName()方法。
  • 类类,以字符串形式返回类的规范名称。
  • 例如,如果Java.lang.Class 对象表示的类的名称是“GFG”,则“getCanonicalName()”方法返回“GFG”。
GFG gfg = new GFG();
System.out.println(gfg.getClass().getCanonicalName());

如果我们在程序中运行上面的代码,那么收到的输出将是:

输出 :

GFG

注意:A.) 如果有包名,那么输出将是“package-name.GFG”。

  • 但对于本地匿名类,情况并非如此。
  • 如果类是成员类,比如说“GFG”是“GeeksForGeeks”的成员类,那么“getCanonicalName()”将返回“GeeksForGeeks.GFG”(形式为“(Name_of_Enclosure_Class).(Name_of_this_Class)”)。

Java

// Java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        // Creating a GeeksForGeeks object
        // to create a GFG object
        GeeksForGeeks geeks = new GeeksForGeeks();
        // Creating a GFG object
        GFG gfg = geeks.new GFG();
  
        // getClass() returns a java.lang.Class
        // instance representing the GFG class
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
  
    class GFG {
        // This is a member class of GeeksForGeeks
    }
}
输出
GeeksForGeeks.GFG

B.) 如果类是类中的匿名类(例如“GFG”),则“ getCanonicalName() ”方法返回 null,因为它没有规范名称。

Java

// java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        GFG gfg = new GFG() {
            // This is an Anonymous class
            // that extends the GFG class
        }; // Remember to put the semicolon here
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
}
输出
null

C.) 如果类是本地内部类,假设“GFG”是“GeeksForGeeks”内部的本地类,那么“getCanonicalName()”也将返回 null。因为,它没有规范名称

Java

// Java program to check if a class is a
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
        class Geeks {
            // This is an another local class
        }
  
        GFG gfg = new GFG();
        Geeks geeks = new Geeks();
  
        System.out.println(
            "Canonical Name of the class of gfg:"
            + gfg.getClass().getCanonicalName());
        System.out.println(
            "Canonical Name of the class of geeks:"
            + geeks.getClass().getCanonicalName());
    }
}
输出
Canonical Name of the class of gfg:null
Canonical Name of the class of geeks:null

使用此规范名称,您可以完成给定的任务。检查以下代码:

Java

// Java program to check if the class
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
  
        GFG gfg = new GFG();
  
        // Storing the class instance associated with gfg
        // i.e. Class instance representing GFG
        Class classInstance = gfg.getClass();
  
        // Checking whether the classInstance has a
        // canonical name
        boolean hasCanonicalName
            = (classInstance.getCanonicalName() != null);
  
        // If hasCanonicalName is false then it is sure that
        // either it is a local class or anonymous class or
        // an array whose component type does not have a
        // canonical name
  
        // Now check whether it is an anonymous Class
        boolean isAnonymousClass
            = classInstance.isAnonymousClass();
  
        // Check if it is an array
        boolean isArray = classInstance.isArray();
  
        // If all the three isArray,isAnonymousClass and
        // hasCanonicalName are false then the classInstance
        // is surely a local class
        boolean isLocalClass
            = (!hasCanonicalName && !isAnonymousClass
               && !isArray);
        System.out.println(
            "Is gfg a local class instance?: "
            + isLocalClass);
    }
}
输出
Is gfg a local class instance?: true