Java Bean simple tutorial

Source: Internet
Author: User

-- Selected from ibatis SQL MAPS Development Guide

Java Bean simple tutorial

The sqlmap architecture requires a solid understanding of Java Beans. Fortunately, there are not many Java Bean APIs related to sqlmap. Therefore, if you have never touched Java Bean before, the following is a brief introduction.

What is Java Bean? Java Bean is a special Java class. it strictly complies with the naming rules of JavaBean and defines the naming rules for access class status information methods.

Java Bean attributes are defined by its methods (rather than by fields ). The method starting with "set" is a writable attribute, while the method starting with "get" is a readable attribute.

For fields of the "Boolean" type, the readable method name can also start with "is. The "set" method should not have a return type (that is, it must be void) and can only have one parameter. The data type of the parameter must be the same as the data type of the attribute. The "get" method should return an appropriate type and no parameters are allowed.

Although not mandatory, the Data Type of the "set" method parameter must be the same as that of the "get" method. Java Bean should also implement the serializable interface.

Java Bean also supports other features (such as events ). However, these features are not important in SQL map and Web applications.

The following is an example of Java Bean:

Public class product implements serializable ...{
Private string ID;

Private string description;

Public String GETID ()...{
Return ID;
}

Public void setid (string ID )...{
This. ID = ID;
}

Public String getdescription ()...{
Return description;
}

Public void setdescription (string description )...{
This. Description = description;
}
Note! Do not confuse the Data Types of the "get" method and "set" method with specific attributes. For example
In the "Account" attribute of the value type, make sure that the getter method and setter method use the same value type, as shown in the following example:
Public void setaccount (int acct ){....}
Public int getaccount (){....}

 

Note that both methods use the "int" type. If the "long" type is returned from the "get" method
Problem.
Note! Similarly, you must have only one "get" method and one "set" method. Be judicious with polymorphic
Methods. You're better off naming them more specifically anyway.
Note! For properties of the "Boolean" type, the "Get" method can also have other syntaxes, which can be "isxxxx ()"
Format. Make sure that you only have one "is" method or "get" method. You cannot write either method at the same time.
Congratulations, you have completed this Java Bean tutorial.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.