用于检查给定类是否为内部类的Java程序
内部类是另一个类的成员,该类基本上是一个非静态嵌套类,即如果一个类在另一个类中并且不是静态的,那么该类被称为内部类。
内部类的类型:
- 嵌套内部类
- 方法局部内部类
- 匿名内部类
- 静态嵌套类
方法一:
- 首先检查给定的类是否是嵌套类
- 进一步检查该类是否为非静态类。
- 如果两个条件都为真,则给定的类是内部类。
- 现在为了检查给定的类是否是嵌套类,使用Java.lang.Class#getEnclosureClass() 方法返回一个 Class 实例,该实例表示对象的直接封闭类。
- 如果该类是顶级类,则该方法将返回 null。
- 这意味着如果类不是嵌套的,则该方法返回 null。
- 此外,使用Java.lang.reflect.Modifier.isStatic() 方法检查给定类是否是静态的。
检查嵌套内部类的程序
Java
// Java Program to Check if a Given Class is an Inner Class
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating as GFG class object
GFG gfg = new GFG();
// Creating a InnerClass object
InnerClass innerClass = gfg.new InnerClass();
// Creating a Static Nested Class object
StaticNestedClass statNested
= new StaticNestedClass();
// getClass() method of an object returns a
// java.lang.Class instance representing the class
// of the object
Class classInstance1 = innerClass.getClass();
Class classInstance2 = statNested.getClass();
// Checking if the given classes are nested
// getEnclosinglClass() of Class instance returns a
// class object representing the immediate enclosing
// class of the instance
boolean isNested1
= classInstance1.getEnclosingClass() != null;
// classInstance1() method returns a Class object
// If the specified class is a top-level class, then
// it would returns null
boolean isNested2
= classInstance2.getEnclosingClass() != null;
// Check if the given classes are static
// getModifiers() method of a class returns the
// modifiers of the class encoded as an integer
// Modifiers.isStatic returns true if the class
// has a static modifier elsereturns false
boolean isStatic1 = Modifier.isStatic(
classInstance1.getModifiers());
boolean isStatic2 = Modifier.isStatic(
classInstance2.getModifiers());
// If the given classes are nested and non-static,
// then given classes are surely inner classes
// Display message
System.out.println(
"Is innerClass an inner class object? : "
+ (isNested1 && !isStatic1));
// Display message
System.out.println(
"Is statNested an inner class object? : "
+ (isNested2 && !isStatic2));
}
// Inner Class
class InnerClass {
}
// Static nested class
static class StaticNestedClass {
}
}
Java
// Java Program to Check if a
// Given Class is an Inner Class
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
class GFG {
public static void main(String[] args)
{
// Creating an anonymous class object
GFG gfg = new GFG() {
// This is an anonymous class that extends class
// GFG
};
class LocalInnerClass {
// This is a local inner class
}
// Creating a LocalInnerClass object
LocalInnerClass innerClass = new LocalInnerClass();
// getClass() method of an object returns a
// instance representing the class of the object
Class classInstance1 = gfg.getClass();
Class classInstance2 = innerClass.getClass();
// Checking if the given classes are nested
// getEnclosinglClass() of Class instance returns a
// class object representing the immediate enclosing
// class of the instance
boolean isNested1
= classInstance1.getEnclosingClass() != null;
// classInstance1() method returns a Class object
// If the specified class is a top-level class, then
// it would returns null
boolean isNested2
= classInstance2.getEnclosingClass() != null;
// Checking if the given classes are nested
// getModifiers() method of a class returns the
// modifiers of the class encoded as an integer
// Modifiers
boolean isStatic1 = Modifier.isStatic(
classInstance1.getModifiers());
// isStatic() returns true if the class has a static
// modifier else returns false
boolean isStatic2 = Modifier.isStatic(
classInstance2.getModifiers());
// Now by this end, classes are nested & non-static
// Then given classes are surely inner classes
// Print the results
System.out.println(
"Is gfg an inner class object? : "
+ (isNested1 && !isStatic1));
System.out.println(
"Is innerClass an inner class object? : "
+ (isNested2 && !isStatic2));
}
}
Java
// Java Program to Check if a Given
// Class is an Inner Class
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a local inner class object
GFG gfg = new GFG() {
};
// getClass() method of an object returns a
// java.lang.CLass
// instance representing the class of the object
Class classInstance = gfg.getClass();
// Checking whether Class is -
// local, member, an anonymous inner class
// and not static
boolean isInnerClass
= (classInstance.isMemberClass()
|| classInstance.isLocalClass()
|| classInstance.isAnonymousClass())
&& !Modifier.isStatic(
classInstance.getModifiers());
// Printing the results
System.out.println(
"Is gfg an inner class object? : "
+ isInnerClass);
}
}
输出
Is innerClass an inner class object? : true
Is statNested an inner class object? : false
同样,对于本地和匿名内部类,上述逻辑返回true。
检查本地和匿名内部类的程序
Java
// Java Program to Check if a
// Given Class is an Inner Class
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
class GFG {
public static void main(String[] args)
{
// Creating an anonymous class object
GFG gfg = new GFG() {
// This is an anonymous class that extends class
// GFG
};
class LocalInnerClass {
// This is a local inner class
}
// Creating a LocalInnerClass object
LocalInnerClass innerClass = new LocalInnerClass();
// getClass() method of an object returns a
// instance representing the class of the object
Class classInstance1 = gfg.getClass();
Class classInstance2 = innerClass.getClass();
// Checking if the given classes are nested
// getEnclosinglClass() of Class instance returns a
// class object representing the immediate enclosing
// class of the instance
boolean isNested1
= classInstance1.getEnclosingClass() != null;
// classInstance1() method returns a Class object
// If the specified class is a top-level class, then
// it would returns null
boolean isNested2
= classInstance2.getEnclosingClass() != null;
// Checking if the given classes are nested
// getModifiers() method of a class returns the
// modifiers of the class encoded as an integer
// Modifiers
boolean isStatic1 = Modifier.isStatic(
classInstance1.getModifiers());
// isStatic() returns true if the class has a static
// modifier else returns false
boolean isStatic2 = Modifier.isStatic(
classInstance2.getModifiers());
// Now by this end, classes are nested & non-static
// Then given classes are surely inner classes
// Print the results
System.out.println(
"Is gfg an inner class object? : "
+ (isNested1 && !isStatic1));
System.out.println(
"Is innerClass an inner class object? : "
+ (isNested2 && !isStatic2));
}
}
输出
Is gfg an inner class object? : true
Is innerClass an inner class object? : true
方法二:
- 使用Java.lang.Class#isLocalClass()、 Java.lang.Class#isMemberClass()、 Java .lang.Class#isAnonymousClass() 方法直接检查类是本地类、成员类还是匿名类。
- 如果给定的类都不是它们,则该类不是内部类。
- 如果该类是其中之一,则使用Java.lang.reflect.Modifier.isStatic() 方法检查该类是否是静态的。
- 当所有条件都满足时打印结果
注意:但是,如果您只想检查类是否是嵌套类,那么您可以简单地使用Java.lang.Class#isLocalClass(), Java.lang.Class#isMemberClass(), Java.lang.Class# isAnonymousClass() 方法。如果它们中的任何一个对给定类返回 true,则可以确定该类是嵌套类。
例子
Java
// Java Program to Check if a Given
// Class is an Inner Class
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a local inner class object
GFG gfg = new GFG() {
};
// getClass() method of an object returns a
// java.lang.CLass
// instance representing the class of the object
Class classInstance = gfg.getClass();
// Checking whether Class is -
// local, member, an anonymous inner class
// and not static
boolean isInnerClass
= (classInstance.isMemberClass()
|| classInstance.isLocalClass()
|| classInstance.isAnonymousClass())
&& !Modifier.isStatic(
classInstance.getModifiers());
// Printing the results
System.out.println(
"Is gfg an inner class object? : "
+ isInnerClass);
}
}
输出
Is gfg an inner class object? : true