Specification for the notation of entities (entity classes) in Java

Source: Internet
Author: User

In the daily development of Java projects, entities (entity classes) are essential, they generally have a lot of attributes, and have the appropriate setter and getter method. Entities (entity classes) generally function as mappings to data tables. Therefore, it is an essential skill in Java development to quickly write out the canonical entity class.

Writing entity classes in a project generally follows the following specifications:

1, according to your design, define a set of private properties you need.

2. Based on these attributes, create their setter and getter methods. (Integrated development software such as eclipse can be generated automatically.) How do I generate the concrete? Please own Baidu. )

3. A constructor with parameters and a parameterless constructor are provided.

4. Override the Eauals () method and the Hashcode () method in the parent class. (These two features are important if you need to relate to comparisons between two objects.) )

5. Implement serialization and give it a version number.

Here is an example of a solid class (entity) I wrote: The details are annotated with annotations.

1 classStudentImplementsserializable{2     /**3 * Version number4      */5     Private Static Final LongSerialversionuid = 1L;6     //private properties defined7     Private intID;8     PrivateString name;9     Private intAge ;Ten     Private Doublescore; One     //constructor with no parameters A      PublicStudent () { -          -     } the     //constructor with parameters -      PublicStudent (intId,string name,intAgeDoublescore) { -          This. ID =ID; -          This. Name =name; +          This. Age =Age ; -          This. score =score; +     } A     //the setter and getter methods created at      Public intgetId () { -         returnID; -     } -      Public voidSetId (intID) { -          This. ID =ID; -     } in      PublicString GetName () { -         returnname; to     } +      Public voidsetName (String name) { -          This. Name =name; the     } *      Public intGetage () { $         returnAge ;Panax Notoginseng     } -      Public voidSetage (intAge ) { the          This. Age =Age ; +     } A      Public DoubleGetscore () { the         returnscore; +     } -      Public voidSetScore (Doublescore) { $          This. score =score; $     } -     //because the ID is uniquely identifiable to students, the hashcode () and Equals () methods of IDs in the parent class are overridden.  - @Override the      Public inthashcode () { -         Final intPrime = 31;Wuyi         intresult = 1; theresult = Prime * result +ID; -         returnresult; Wu     } - @Override About      Public Booleanequals (Object obj) { $         if( This==obj) -             return true; -         if(obj = =NULL) -             return false; A         if(GetClass ()! =Obj.getclass ()) +             return false; theStudent other =(Student) obj; -         if(id! =other.id) $             return false; the         return true; the     } the      the}

A student's Java entity class is basically done.

Specification for the notation of entities (entity classes) in Java

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.