Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection to troubleshoot negative numbers in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report