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