📌  相关文章
📜  Java的.lang.reflect.Modifier类在Java中

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

Java的.lang.reflect.Modifier类在Java中

Java.lang.reflect.Modifier 类包含用于获取有关类、成员和方法访问修饰符的信息的方法。修饰符表示为 int 值,在不同的位置设置位。 int 值代表不同的修饰符。这些值取自 JVM 规范第 4.1、4.4、4.5 和 4.7 节中的表。

类声明:

Public class modifier extends Object

领域:

FieldDescription
ABSTRACT Integer value representing the abstract modifier.
FINAL Integer value representing the final modifier.
INTERFACE Integer value representing the interface modifier.
NATIVE Integer value representing the native modifier.
PRIVATE Integer value representing the private modifier.
PROTECTED Integer value representing the protected modifier.
PUBLIC Integer value representing the public modifier.
STATIC Integer value representing the static modifier.
STRICT Integer value representing the strictfp modifier.
SYNCHRONIZED Integer value representing the synchronized modifier.
TRANSIENT Integer value representing the transient modifier.
VOLATILE Integer value representing the volatile modifier.

构造函数:

public Modifier(): Default constructor

方法:



MethodDescription
classModifiers()This method returns an int value OR-ing together the source language modifiers that can be applied to a class.
constructorModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to a constructor.
fieldModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to a field.
interfaceModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to an interface.
isAbstract(int mod)This method returns true if the integer argument includes the abstract modifier, false otherwise.
isFinal(int mod) This method returns true if the integer argument includes the final modifier, false otherwise.
isInterface(int mod) This method returns true if the integer argument includes the interface modifier, false otherwise.
isNative(int mod) This method returns true if the integer argument includes the native modifier, false otherwise.
isPrivate(int mod) This method returns true if the integer argument includes the private modifier, false otherwise.
isProtected(int mod) This method returns true if the integer argument includes the protected modifier, false otherwise.
isPublic(int mod) This method returns true if the integer argument includes the public modifier, false otherwise.
isStatic(int mod) This method returns true if the integer argument includes the staticfp modifier, false otherwise.
isSynchronized(int mod) This method returns true if the integer argument includes the synchronized modifier, false otherwise.
isTransient(int mod) This method returns true if the integer argument includes the transient modifier, false otherwise.
isVolatile(int mod) This method returns true if the integer argument includes the volatile modifier, false otherwise.
methodModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to a method.
parameterModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to a parameter.
toString(int mod) This method returns a string describing the access modifier flags in the specified modifier.

1. 静态 int classModifiers():

此方法在对可应用于类的访问修饰符执行 OR 操作后返回一个 int 值。

Return type: Integer
Java
// Implementation of classModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.classModifiers());
        System.out.println(
            Modifier.toString(Modifier.classModifiers()));
    }
}


Java
// Implementation of constructorModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.constructorModifiers());
        System.out.println(Modifier.toString(
            Modifier.constructorModifiers()));
    }
}


Java
// Implementation of fieldModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.fieldModifiers());
        System.out.println(Modifier.toString(
            Modifier.fieldModifiers()));
    }
}


Java
// Implementation of interfaceModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.interfaceModifiers());
        System.out.println(Modifier.toString(
            Modifier.interfaceModifiers()));
    }
}


Java
// Implementation of isAbstract() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isAbstract(
            demoClass.class.getModifiers()));
    }
    abstract class demoClass {
    }
}


Java
// Implementation of isFinal() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isFinal(
            demoClass.class.getModifiers()));
    }
    final class demoClass {
    }
}


Java
// Implementation of isInterface() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isInterface(
            demoInterface.class.getModifiers()));
    }
    interface demoInterface {
        String demoMethod();
    }
}


Java
// Implementation of isNative() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG0 {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isNative(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}


Java
// Implementation of isPrivate() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPrivate(
            demoClass.class.getModifiers()));
    }
    private class demoClass {
    }
}


Java
// Implementation of isProtected() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isProtected(
            demoClass.class.getModifiers()));
    }
    protected class demoClass {
    }
}


Java
// Implementation of isPublic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPublic(
            demoClass.class.getModifiers()));
    }
    public class demoClass {
    }
}


Java
// Implementation of isStatic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isStatic(
            demoClass.class.getModifiers()));
    }
    static class demoClass {
    }
}


Java
// Implementation of isStrict() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isStrict(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}


Java
// Implementation of isSynchronized() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(Modifier.isSynchronized(
            methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final synchronized String getDemoField()
        {
            return demoField;
        }
    }
}


Java
// Implementation of isTransient() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isTransient(field.getModifiers()));
    }
    static class demoClass {
        public transient String demoField;
  
        public final native String getDemoField();
    }
}


Java
// Implementation of isVolatile() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isVolatile(field.getModifiers()));
    }
    static class demoClass {
        public volatile String demoField;
  
        public final native String getDemoField();
    }
}


Java
// Implementation of methodModifiers() Method
import java.lang.reflect.Modifier;
  
public class modifier {
    public static void main(String[] args)
    {
        System.out.println(Modifier.methodModifiers());
    }
}


输出
3103
public protected private abstract static final strictfp

2. static int constructorModifiers();对可应用于构造函数的访问修饰符执行 OR 运算后返回一个 int 值。

Return type: Integer

Java

// Implementation of constructorModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.constructorModifiers());
        System.out.println(Modifier.toString(
            Modifier.constructorModifiers()));
    }
}
输出
7
public protected private

3. static int fieldModifiers():对可应用于字段的访问修饰符进行OR运算后,返回一个int值。

Return type: Integer

Java

// Implementation of fieldModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.fieldModifiers());
        System.out.println(Modifier.toString(
            Modifier.fieldModifiers()));
    }
}
输出
223
public protected private static final transient volatile

4. static int interfaceModifiers():对可应用于接口的访问修饰符进行OR运算后,返回一个int值。



Return type: Integer

Java

// Implementation of interfaceModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.interfaceModifiers());
        System.out.println(Modifier.toString(
            Modifier.interfaceModifiers()));
    }
}
输出
3087
public protected private abstract static strictfp

6. static boolean isAbstract():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isAbstract() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isAbstract(
            demoClass.class.getModifiers()));
    }
    abstract class demoClass {
    }
}
输出
true

7. static boolean isFinal():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isFinal() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isFinal(
            demoClass.class.getModifiers()));
    }
    final class demoClass {
    }
}
输出
true

8. static boolean isInterface():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isInterface() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isInterface(
            demoInterface.class.getModifiers()));
    }
    interface demoInterface {
        String demoMethod();
    }
}
输出
true

9. static boolean isNative():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isNative() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG0 {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isNative(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}
输出
true

10. static boolean isPrivate():检查整数参数是否包含abstract修饰符。



Return type: boolean

Java

// Implementation of isPrivate() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPrivate(
            demoClass.class.getModifiers()));
    }
    private class demoClass {
    }
}
输出
true

11. static boolean isProtected():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isProtected() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isProtected(
            demoClass.class.getModifiers()));
    }
    protected class demoClass {
    }
}
输出
true

12. static boolean isPublic():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isPublic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPublic(
            demoClass.class.getModifiers()));
    }
    public class demoClass {
    }
}
输出
true

13. static boolean isStatic():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isStatic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isStatic(
            demoClass.class.getModifiers()));
    }
    static class demoClass {
    }
}
输出
true

14. static boolean isStrict():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isStrict() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isStrict(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}
输出
false

15. static boolean isSynchronized():检查整数参数是否包含abstract修饰符。



Return type: boolean

Java

// Implementation of isSynchronized() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(Modifier.isSynchronized(
            methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final synchronized String getDemoField()
        {
            return demoField;
        }
    }
}
输出
true

16. static boolean isTransient():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isTransient() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isTransient(field.getModifiers()));
    }
    static class demoClass {
        public transient String demoField;
  
        public final native String getDemoField();
    }
}
输出
true

17. static boolean isVolatile():检查整数参数是否包含abstract修饰符。

Return type: boolean

Java

// Implementation of isVolatile() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isVolatile(field.getModifiers()));
    }
    static class demoClass {
        public volatile String demoField;
  
        public final native String getDemoField();
    }
}
输出
true

18. static int methodModifiers():对可应用于方法的访问修饰符的值进行OR运算后,返回一个int值。

Return type: Integer

Java

// Implementation of methodModifiers() Method
import java.lang.reflect.Modifier;
  
public class modifier {
    public static void main(String[] args)
    {
        System.out.println(Modifier.methodModifiers());
    }
}
输出
3391