[英]Determine whether member class is nested or inner?
Suppose I have a class that contains come member classes, some of them nested (static) and some inner (non-static): 假设我有一个包含来自成员类的类,其中一些嵌套(静态)和一些内部(非静态):
class Foo {
static class A {}
class B {}
static class C {}
}
I would like to get all the nested member classes of Foo
, ie A
and C
, but not B
. 我想获得所有嵌套的Foo
成员类,即A
和C
,但不是B
Is this even possible? 这甚至可能吗? I know how to list all the member classes (using Foo.class.getDesclaredClasses()
), i see methods such as isLocalClass()
, isMemberClass()
or isAnonymousClass()
on java.lang.Class
, but I don't see anything like isNested()
or isInner()
. 我知道如何列出所有成员类(使用Foo.class.getDesclaredClasses()
),我在java.lang.Class
上看到了诸如isLocalClass()
, isMemberClass()
或isAnonymousClass()
,但我没有看到任何内容像isNested()
或isInner()
。
You can use Modifier#isStatic(int mod)
( Return true if the integer argument includes the static modifier, false otherwise. ) to know if the inner-class is static or not. 您可以使用Modifier#isStatic(int mod)
( 如果整数参数包含static修饰符,则返回true,否则返回false。 )以了解内部类是否为静态。
Like this : 像这样 :
public class Demo {
public static void main(String[] args) {
Class foo = Foo.class;
Class[] declaredClasses = foo.getDeclaredClasses();
for (Class class1 : declaredClasses) {
System.out.println(Modifier.isStatic(class1.getModifiers()));
}
}
}
class Foo {
static class A {}
class B {}
static class C {}
}
true
false
true
Note : Class#getModifiers()
- 注意: Class#getModifiers()
-
Returns the Java language modifiers for this class or interface, encoded in an integer. 返回此类或接口的Java语言修饰符,以整数编码。 The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface; 修饰符由Java虚拟机的公共,受保护,私有,最终,静态,抽象和接口的常量组成; they should be decoded using the methods of class Modifier. 它们应该使用类Modifier的方法进行解码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.