getmodifiers_Java类类getModifiers()方法及示例

getmodifiers

类的类getModifiers()方法 (Class class getModifiers() method)

  • getModifiers() method is available in java.lang package.

    getModifiers()方法在java.lang包中可用。

  • getModifiers() method is used to return the modifiers for this class or interfaces and the modifiers consist of JVM constants for the modifiers public, private, protected, final, static, abstract and interface and these modifiers should be decoded by the method getModifier() of Modifier class.

    getModifiers()方法用于返回此类或接口的修饰符,并且修饰符由JVM常数组成,这些修饰符用于修饰符public,private,protected,final,static,abstract和interface,这些修饰符应由getModifier()方法解码修饰符类。

  • getModifiers() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getModifiers()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getModifiers() method does not throw an exception at the time of encoding a modifier.

    在对修饰符进行编码时, getModifiers()方法不会引发异常。

Syntax:

句法:

    public int getModifiers();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is int, it returns an integer value representing the modifiers for the class or an interfaces.

此方法的返回类型为int ,它返回一个整数值,该整数值表示类或接口的修饰符。

Example:

例:

// Java program to demonstrate the example 
// of int getModifiers () method of Class 

import java.lang.reflect.*;

public class GetModifiersOfClass {
    public static void main(String[] args) {
        Thread th = new Thread();

        // Get Class object of Thread
        Class cl = th.getClass();

        // It return the modifier of the class Thread
        int modifiers = cl.getModifiers();

        // Display Modifier in integer form 
        System.out.println("Before Conversion = " + modifiers);

        // Convert Modifer from int to String by using toString()
        String modifier = Modifier.toString(modifiers);

        // Display Modifier in String form 
        System.out.print("After Conversion = " + modifier);
    }
}

Output

输出量

Before Conversion = 1
After Conversion = public


翻译自: https://www.includehelp.com/java/class-class-getmodifiers-method-with-example.aspx

getmodifiers

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用反射机制来动态创建类对象并生成类。下面是一个示例代码,可以自动化生成一个名为"Person"的类,该类包含两个私有属性name和age,以及两个公有方法getName和getAge。 ```java import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class ClassGenerator { public static void main(String[] args) throws Exception { // 创建一个名为"Person"的类 Class<?> clazz = Class.forName("Person"); // 创建name属性 Field nameField = clazz.getDeclaredField("name"); nameField.setAccessible(true); // 创建age属性 Field ageField = clazz.getDeclaredField("age"); ageField.setAccessible(true); // 创建getName方法 Method getNameMethod = clazz.getMethod("getName"); getNameMethod.setAccessible(true); // 创建getAge方法 Method getAgeMethod = clazz.getMethod("getAge"); getAgeMethod.setAccessible(true); // 打印生成的类信息 System.out.println("生成的类信息:"); System.out.println("类名:" + clazz.getName()); System.out.println("属性:" + Modifier.toString(nameField.getModifiers()) + " " + nameField.getType().getName() + " " + nameField.getName() + ";"); System.out.println("属性:" + Modifier.toString(ageField.getModifiers()) + " " + ageField.getType().getName() + " " + ageField.getName() + ";"); System.out.println("方法:" + Modifier.toString(getNameMethod.getModifiers()) + " " + getNameMethod.getReturnType().getName() + " " + getNameMethod.getName() + "();"); System.out.println("方法:" + Modifier.toString(getAgeMethod.getModifiers()) + " " + getAgeMethod.getReturnType().getName() + " " + getAgeMethod.getName() + "();"); } static class Person { private String name; private int age; public String getName() { return name; } public int getAge() { return age; } } } ``` 运行以上代码,会输出以下结果: ``` 生成的类信息: 类名:Person 属性:private java.lang.String name; 属性:private int age; 方法:public java.lang.String getName(); 方法:public int getAge(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值