Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use reflection to troubleshoot negative numbers in Java

Shulou Source: shulou.com Published: 2022-06-02 02:58:36 09月23日 Update

This article mainly introduces how to use reflection in Java to troubleshoot negative numbers, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

1. I haven't written about reflection for a long time. There are many ways to forget the following check column for negative numbers. It's not very good. Let's put it together.

Public class NumberUtil {/ * returns the object property, which is double and cannot be negative. A negative number will remind * @ Title:checkNumFilter * @ Description:TODO * @ Param:@param obj * @ Param:@return * @ return: boolean * @ throws SecurityException * @ throws NoSuchMethodException * @ throws InvocationTargetException * @ throws IllegalArgumentException * @ throws IllegalAccessException * @ throws * @ Author: Kuchawyz October 10, 2019 * / public static boolean checkNumFilter (Object obj) throws NoSuchMethodException SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {/ / TODO Auto-generated method stub if (obj! = null) {Class clz = obj.getClass () Field [] filds = clz.getDeclaredFields () For (Field field: filds) {if (field.getGenericType () .toString () .equals ("double")) {String methodName = "get" + upperCase (field.getName ()) Method m = obj.getClass () .getDeclaredMethod (methodName); doubleval = (double) m.invoke (obj); if (val)

< 0){ throw new ErrorFormateException("你传入的数字有负数的"); } } } return true; } return false; } /** * 对给定的字符串首字母变成大写 * (这里先将字符串转为字符数组, * 然后将数组的第一个元素,即字符串首字母,进行ASCII 码前移, * ASCII 中大写字母从65开始,小写字母从97开始,所以这里减去32) *@Title:upperCase *@Description:TODO * @Param:@param str * @Param:@return *@return :String *@throws *@author :kuchawyz2019年10月10日 */ public static String upperCase(String str) { char[] ch = str.toCharArray(); if (ch[0] >

='a'& & ch [0]

Tags: Negative number reflection content object attribute article value interest guy partner method more knowledge industry information information channel channel Liezi reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple MariaDB macOS OPPO Reno