📌  相关文章
📜  Java中的修饰符 isFinal(mod) 方法及示例

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

Java中的修饰符 isFinal(mod) 方法及示例

Java.lang.reflect.ModifierisFinal(mod)方法用于检查整数参数是否包含 final 修饰符。如果此整数参数表示最终类型修饰符,则方法返回 true,否则返回 false。

句法:

public static boolean isFinal(int mod)

参数:此方法接受一个整数名称,因为 mod 表示一组修饰符。

Return :如果 mod 包含 final 修饰符,此方法返回 true;否则为假。

下面的程序说明了 isFinal() 方法:
方案一:

// Java program to illustrate isFinal() method
  
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // get Method class object
        Method[] methods
            = Shape.class.getMethods();
  
        // get Modifier Integer value
        int mod = methods[0].getModifiers();
  
        // check Modifier is final or not
        boolean result = Modifier.isFinal(mod);
  
        System.out.println("Mod integer value "
                           + mod + " is final : "
                           + result);
    }
  
    class Shape {
  
        public final void drawShape() {}
    }
}
输出:
Mod integer value 17 is final : true

方案二:

// Java program to illustrate isFinal()
  
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // get Method class object
        Method[] methods
            = Numbers.class.getMethods();
  
        // get Modifier Integer value
        int mod1 = methods[0].getModifiers();
        int mod2 = methods[1].getModifiers();
  
        // check Modifiers are final or not
        boolean result1 = Modifier.isFinal(mod1);
        boolean result2 = Modifier.isFinal(mod2);
  
        // print results
        System.out.println("Mod integer value "
                           + mod1 + " for method "
                           + methods[0].getName()
                           + " is final : "
                           + result1);
  
        System.out.println("Mod integer value "
                           + mod2 + " for method"
                           + methods[1].getName()
                           + " is final : "
                           + result2);
    }
  
    // sample final class
    abstract class Numbers {
  
        public abstract int initializeNumber();
        public final int declareNumbers()
        {
            return 0;
        }
    }
}
输出:
Mod integer value 1025 for method initializeNumber is final : false
Mod integer value 17 for methoddeclareNumbers is final : true

参考资料: https: Java/lang/reflect/Modifier.html#isFinal(int)