简体   繁体   English

getModifiers()方法如何计算多个修饰符的值?

[英]How does the getModifiers() method calculate the value for multiple modifiers?

The Java Doc for getModifiers() is as follows: getModifiers()的Java Doc如下:

int getModifiers() int getModifiers()

Returns the Java language modifiers for the member or constructor represented by this Member, as an integer. 以整数形式返回此Member表示的成员或构造函数的Java语言修饰符。 The Modifier class should be used to decode the modifiers in the integer. 应使用Modifier类来解码整数中的修饰符。

and Java Docs also provide the list of different modifiers and their corresponding int values : 和Java Docs还提供了不同修饰符及其对应的int值列表

public static final int ABSTRACT 1024 public static final int ABSTRACT 1024

public static final int FINAL 16 public static final int FINAL 16

public static final int INTERFACE 512 public static final int INTERFACE 512

public static final int NATIVE 256 public static final int NATIVE 256

public static final int PRIVATE 2 public static final int PRIVATE 2

public static final int PROTECTED 4 public static final int PROTECTED 4

public static final int PUBLIC 1 public static final int PUBLIC 1

public static final int STATIC 8 public static final int STATIC 8

public static final int STRICT 2048 public static final int STRICT 2048

public static final int SYNCHRONIZED 32 public static final int SYNCHRONIZED 32

public static final int TRANSIENT 128 public static final int TRANSIENT 128

public static final int VOLATILE 64 public static final int VOLATILE 64

For a single modifier it is very straightforward for getModifiers(). 对于单个修饰符,getModifiers()非常简单。 It simply returns the constant value corresponding to the modifier (eg when I declare one class as public final and call the getModifiers() it returns 17 ). 它只返回对应于修饰符的常量值(例如,当我将一个类声明为public final并调用getModifiers()时,它返回17 )。

System.out.println("modifierValue:" + MyClass.class.getModifiers());

Output is: 输出是:

17

However, I can't quite understand how it would work for multiple modifiers. 但是,我不太明白它如何适用于多个修饰符。 Can anyone enlighten me? 任何人都可以开导我吗?

TL;DR: It adds them together to form a bit field . TL; DR:它将它们加在一起形成一个位字段

To understand this you need to understand how binary works, this is similar to decimal - lets start there: 要理解这一点,你需要了解二进制如何工作,这类似于十进制 - 让我们从那里开始:

 1    - public
 10   - static
 100  - final

So, what does 101 mean? 那么, 101意味着什么? It must be public final because there is no other way in the decimal system to make "one hundred and one" except with a single 100 and a single 1 . 它必须是public final因为除了单个100和单个1之外,十进制系统中没有其他方法可以制作“一百零一”。

Now extend this to binary: 现在将其扩展为二进制:

1 - public
2 - private
4 - protected
8 - static

So what does 9 mean? 9是什么意思? Well, as with the decimal system there is only one (correct) way to make 9 in binary - one 8 and one 1 . 好吧,与十进制系统一样,只有一种(正确的)方法可以使二进制9 - 一个8和一个1

Now we use what we call a bitfield, 9 in binary is: 现在我们使用我们称之为位域的内容,二进制中的9是:

1001

To verify, write some code ! 要验证, 写一些代码

public static void main(String[] args) throws Exception {
    int i = 9;
    System.out.println(Integer.toBinaryString(i));
}

Now, with the decimal system we would divide by 10 repeatedly and check the right most digit (least significant). 现在,使用十进制系统,我们将重复除以10并检查最右边的数字(最不重要)。 With binary this process is identical, except we divide by 2 - this is known as bit shifting. 对于二进制,这个过程是相同的,除了我们除以2 - 这被称为位移。

public static void main(String[] args) throws Exception {
    int i = 9;
    System.out.println(Integer.toBinaryString(i));
    i >>= 1;
    System.out.println(Integer.toBinaryString(i));
    i >>= 1;
    System.out.println(Integer.toBinaryString(i));
    i >>= 1;
    System.out.println(Integer.toBinaryString(i));
}

Output: 输出:

1001
100
10
1

So, if I know that private is the 2 1 value, and we know how many bits we have, all just need to shift the right number of digits and take the modulo with 2 : 所以,如果我知道private是2 1值,并且我们知道我们有多少位,那么只需要移位正确的位数并将模数取为2

public static void main(String[] args) throws Exception {
    int i = 9;
    i >>= 2;
    System.out.println(i%2);
}

Output: 输出:

0

So we basically use the 1 and 0 values that make up a number in binary to number to store booleans. 所以我们基本上使用构成二进制数的10值来编号来存储布尔值。

So take this example into the read world: 所以把这个例子带入阅读世界:

public static void main(String[] args) throws Exception {
    final Method method = App.class.getMethod("myMethod");
    final int modifiers = method.getModifiers();
    System.out.println(modifiers);
    System.out.println(Integer.toBinaryString(modifiers));
}

public strictfp synchronized static final void myMethod() {

}

Output: 输出:

2105
100000111001

So we can see we have: 所以我们可以看到我们有:

2 0 = 1 - true 2 0 = 1 - true
2 1 = 2 - false 2 1 = 2 - false
2 2 = 4 - false 2 2 = 4 - false
2 3 = 8 - true 2 3 = 8 - true
2 4 = 16 - true 2 4 = 16 - true
2 5 = 32 - true 2 5 = 32 - true
2 7 = 64 - false 2 7 = 64 - false
2 8 = 128 - false 2 8 = 128 - false
2 9 = 256 - false 2 9 = 256 - false
2 10 = 512 - false 2 10 = 512 - false
2 11 = 1024 - false 2 11 = 1024 - false
2 12 = 2048 - true 2 12 = 2048 - true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM