集册 Java实例教程 假设方法的属性的返回名称是变量或访问器。

假设方法的属性的返回名称是变量或访问器。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

426
假设方法的属性的返回名称是变量或访问器。
// 来自 时代Java - nowjava.com

//package com.nowjava;

import java.lang.reflect.Method;


public class Main {

    /** Prefix for all isser accessor methods. */

    private static final String IS_ACCESSOR_PREFIX = "is";


    /**

     * Return name of attribute assuming method is a mutator or accessor.

     *

     * @param method the method.

     * @return the attribute name.

     */

    public static String getAttributeName(final Method method) {

        final String methodName = method.getName();

        final boolean isserAccessor = methodName

                .startsWith(IS_ACCESSOR_PREFIX);

        final String name;

        if (isserAccessor) {

            name = Character.toLowerCase(methodName.charAt(2))

                    + methodName.substring(3);

        } 
展开阅读全文