java.lang.Class.getFields()方法实例

算法 ww9992019com 发表于 1 周前

www.lyjust.net开户请拨18288069766张柏芝小儿子首曝金华婺城区VwTIjp<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="4533">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

<strong>java.lang.Class.getFields() </strong>返回一个包含Field对象反映此Class对象(object)方法所表示的类或接口的所有可访问公共字段的数组返回一个长度为0的数组,如果类或接口没有可利用的公有字段,或者如果它表示一个数组类、基本类型或void。</p>

<h2>

声明</h2>

<p>

以下是<strong>java.lang.Class.getFields()</strong>方法的声明</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="typ">Field</span><span class="pun">[]</span><span class="pln"> getFields</span><span class="pun">()</span><span class="pln"> </span><span class="kwd">throws</span><span class="pln"> </span><span class="typ">SecurityException</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        NA</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

此方法返回一个代表公共字段Field对象的数组。</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        SecurityException -- 如果安全管理存在。</p>

</li>

</ul>

<h2>

例子</h2>

<p>

下面的例子显示java.lang.Class.getFields()方法的使用。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="pln">reflect</span><span class="pun">.*;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ClassDemo</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

 </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

    </span><span class="typ">Class</span><span class="pln"> cls </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Class</span><span class="pun">.</span><span class="pln">forName</span><span class="pun">(</span><span class="str">"java.awt.Label"</span><span class="pun">);</span><span class="pln">

    </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Fields ="</span><span class="pun">);</span><span class="pln">



    </span><span class="com">// returns the array of Field objects representing the public fields</span><span class="pln">

    </span><span class="typ">Field</span><span class="pln"> f</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> cls</span><span class="pun">.</span><span class="pln">getFields</span><span class="pun">();</span><span class="pln">

    </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun">&lt;</span><span class="pln"> f</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

       </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">f</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span><span class="pln">

    </span><span class="pun">}</span><span class="pln">

 </span><span class="pun">}</span><span class="pln">

 </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Exception</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

    </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Exception: "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> e</span><span class="pun">);</span><span class="pln">

 </span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln"> </span></pre>

<p>

让我们来编译和运行上面的程序,这将产生以下结果:</p>

<pre class="prettyprint prettyprinted">

<span class="typ">Fields</span><span class="pln"> </span><span class="pun">=</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Label</span><span class="pun">.</span><span class="pln">LEFT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Label</span><span class="pun">.</span><span class="pln">CENTER

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Label</span><span class="pun">.</span><span class="pln">RIGHT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Component</span><span class="pun">.</span><span class="pln">TOP_ALIGNMENT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Component</span><span class="pun">.</span><span class="pln">CENTER_ALIGNMENT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Component</span><span class="pun">.</span><span class="pln">BOTTOM_ALIGNMENT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Component</span><span class="pun">.</span><span class="pln">LEFT_ALIGNMENT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">float</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="typ">Component</span><span class="pun">.</span><span class="pln">RIGHT_ALIGNMENT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">WIDTH

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">HEIGHT

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">PROPERTIES

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">SOMEBITS

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">FRAMEBITS

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">ALLBITS

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">ERROR

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">awt</span><span class="pun">.</span><span class="pln">image</span><span class="pun">.</span><span class="typ">ImageObserver</span><span class="pun">.</span><span class="pln">ABORT</span></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="java.lang.Class.getField()方法实例"><i class="layui-icon"></i>
                            java.lang.Class.getField()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:java.lang.Class.getGenericInterfaces()方法实例"><i class="layui-icon"></i>
                            java.lang.Class.getGenericInterfaces()方法实例</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复