gpt4 book ai didi

java - 如何打印一种 setter/getter

转载 作者:行者123 更新时间:2023-11-30 03:19:50 24 4
gpt4 key购买 nike

我有如下代码:

public static void main(String[] args) throws Exception {
printGettersSetters(PodmiotSzukajQueryFormSet.class);
}

public static void printGettersSetters(Class aClass) {
Method[] methods = aClass.getMethods();

for (Method method: methods) {
if (isGetter(method)) {
System.out.println("getter: " + method);
}
}
}

public static boolean isGetter(Method method) {
if (!method.getName().startsWith("getSomething")) {
return false;
}
if (method.getParameterTypes().length != 0) return false;
if (void.class.equals(method.getReturnType())) return false;
return true;
}

输出:

getter: public package.package.Class package.package.Class.getSomething()

如何获得该 getter 的类型,在本例中为:“Class”,但在其他示例中为“String”、“int”等。

--编辑有可能.getModifiers() 但它返回 int。如何返回字符串?

最佳答案

想想你在寻找什么方法getReturnType()对象的 java.lang.reflect.Method

public Class getReturnType()

Returns a Class object that represents the formal return type of the method represented by this Method object.

Returns:the return type for the method this object represents

关于java - 如何打印一种 setter/getter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com