In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
GetDeclaredField () method and NoSuchFieldException exception handling, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Using the getDeclaredField () method, an exception NoSuchFieldException is found, and the subsequent discovery is caused by the entity class using extends, so Baidu gives the following reasons.
It is found that the getDeclaredField () method can only get all the properties of the current class, but not the parent class.
This leads to two questions. The first question is what the getField () and getDeclaredField () methods do:
The getField (String name) or getDelaredField (String name) of Class is used to get the specified property of the target class, and the return type is Field.
The second question: what's the difference between the getField () and getDeclaredField () methods:
GetField (String name) can only get fields of public, including those of the parent class
GetDeclaredField (String name) can only get various fields declared by itself, including public,protected,private.
So there are several ways to solve the problems I encounter:
Method 1: deprecate extends, but what if I want to continue using extends? look at method 2.
Method 2: you can get the parent class through the getSuperclass () method, and then call the getDeclaredField () method.
Field field;try {field = obj.getClass () .getDeclaredField (fieldName);} catch (NoSuchFieldException e) {/ / this is used to solve the problem that getDeclaredField cannot directly obtain parent class attributes caused by inheritance field = obj.getClass () .getSuperclass () .getDeclaredField (fieldName) } the answers to the questions about the getDeclaredField () method and NoSuchFieldException exception handling are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.