JavaBean命名规范

本文详细介绍了 JavaBean 的规范要求,包括默认构造方法、属性的 getter 和 setter 方法及命名约定,强调了 JavaBean 类必须实现序列化接口的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://en.wikipedia.org/wiki/JavaBean#JavaBean_conventions

JavaBean conventions[edit]

In order to function as a JavaBean class, an object class must obey certainconventions about method naming, construction, and behaviour. These conventionsmake it possible to have tools that can use, reuse, replace, and connectJavaBeans.

The required conventions are as follows:

·        The class must have a public defaultconstructor (with no-arguments). This allows easyinstantiation within editing and activation frameworks.

·        The class properties must be accessible using getsetis (used for boolean properties instead ofget), and other methods (so-called accessor methods and mutator methods) according to a standard namingconvention. This allows easy automated inspection and updating of bean state withinframeworks, many of which include custom editors for various types ofproperties. Setters can have one or more than one argument.

·        The class should be serializable. [This allows applications and frameworksto reliably save, store, and restore the bean's state in a manner independentof the VM and of the platform.]

 

package player;

 

public class PersonBean implements java.io.Serializable {

 

    private String name = null;

 

    private boolean deceased = false;

 

    /** No-arg constructor (takes no arguments). */

    public PersonBean() {

    }

 

    /**

     * Property <code>name</code>(note capitalization) readable/writable.

     */

    public String getName() {

       return name;

    }

 

    /**

     * Setter for property <code>name</code>.

     * @param NAME®

     */

    public void setName(final String NAME) {

       name = NAME;

    }

 

    /**

     * Getter for property "deceased"

     * Different syntax for a boolean field (isvs. get)

     */

    public boolean isDeceased() {

       return deceased;

    }

 

    /**

     * Setter for property<code>deceased</code>.

     * @param DECEASED

     */

    public void setDeceased(final boolean DECEASED) {

       deceased = DECEASED;

    }

}

TestPersonBean.java:

import beans.PersonBean;

 

/**

 * Class<code>TestPersonBean</code>.

 */

public class TestPersonBean {

    /**

     * Tester method<code>main</code> for class <code>PersonBean</code>.

     * @param ARGS

     */

    public static void main(String[] ARGS) {

       PersonBean PERSON = new PersonBean();

 

       PERSON.setName("Bob");

       PERSON.setDeceased(false);

 

       // Output: "Bob [alive]"

       System.out.print(PERSON.getName());

       System.out.println(PERSON.isDeceased() ? "[deceased]" : " [alive]");

    }

}

testPersonBean.jsp;

<% // Use of PersonBean in a JSP. %>

<jsp:useBean id="person"class="beans.PersonBean"scope="page"/>

<jsp:setPropertyname="person" property="*"/>

 

<html>

    <body>

       Name: <jsp:getProperty name="person" property="name"/><br/>

       Deceased? <jsp:getProperty name="person" property="deceased"/><br/>

       <br/>

       <formname="beanTest"method="POST"action="testPersonBean.jsp">

           Enter a name: <inputtype="text"name="name"size="50"><br/>

           Choose an option:

           <selectname="deceased">

                <optionvalue="false">Alive</option>

                <optionvalue="true">Dead</option>

           </select>

           <inputtype="submit"value="Test the Bean">

       </form>

    </body>

</html>

JavaBeanconventions[edit]

In order to function as a JavaBean class, an object class must obey certainconventions about method naming, construction, and behaviour. These conventionsmake it possible to have tools that can use, reuse, replace, and connectJavaBeans.

The required conventions are as follows:

·        The class must have a public defaultconstructor (with no-arguments). This allows easyinstantiation within editing and activation frameworks.

·        The class properties must be accessible using getsetis (used for boolean properties instead of get),and other methods (so-called accessor methods and mutator methods) according to a standard namingconvention. This allows easy automated inspection and updating of bean state withinframeworks, many of which include custom editors for various types ofproperties. Setters can have one or more than one argument.

·        The class should be serializable. [This allows applications and frameworksto reliably save, store, and restore the bean's state in a manner independentof the VM and of the platform.]

 

package player;

 

public class PersonBean implements java.io.Serializable {

 

    private String name = null;

 

    private boolean deceased = false;

 

    /** No-argconstructor (takes no arguments). */

    public PersonBean() {

    }

 

    /**

    * Property <code>name</code> (note capitalization)readable/writable.

    */

    public String getName() {

        return name;

    }

 

    /**

    * Setter for property <code>name</code>.

    * @param NAME®

    */

    public void setName(final String NAME) {

        name = NAME;

    }

 

    /**

    * Getter for property "deceased"

    * Different syntax for a boolean field (is vs. get)

    */

    public boolean isDeceased() {

        return deceased;

    }

 

    /**

    * Setter for property <code>deceased</code>.

    * @param DECEASED

    */

    public void setDeceased(final boolean DECEASED) {

        deceased = DECEASED;

    }

}

TestPersonBean.java:

import beans.PersonBean;

 

/**

 *Class <code>TestPersonBean</code>.

 */

public class TestPersonBean {

    /**

    * Tester method <code>main</code> for class<code>PersonBean</code>.

    * @param ARGS

    */

    public static void main(String[] ARGS) {

        PersonBean PERSON = new PersonBean();

 

        PERSON.setName("Bob");

        PERSON.setDeceased(false);

 

        // Output:"Bob [alive]"

        System.out.print(PERSON.getName());

        System.out.println(PERSON.isDeceased() ? " [deceased]" : " [alive]");

    }

}

testPersonBean.jsp;

<% // Use ofPersonBean in a JSP. %>

<jsp:useBean id="person"class="beans.PersonBean"scope="page"/>

<jsp:setProperty name="person" property="*"/>

 

<html>

    <body>

        Name: <jsp:getPropertyname="person" property="name"/><br/>

        Deceased? <jsp:getPropertyname="person" property="deceased"/><br/>

        <br/>

        <formname="beanTest"method="POST"action="testPersonBean.jsp">

            Enter a name: <inputtype="text"name="name"size="50"><br/>

            Choose an option:

            <selectname="deceased">

                <optionvalue="false">Alive</option>

                <optionvalue="true">Dead</option>

            </select>

            <inputtype="submit"value="Test theBean">

        </form>

    </body>

</html>

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值