Detailed explanation of the function of entity class entity in Android project

  • 2021-12-04 11:02:35
  • OfStack

It is estimated that many friends who are getting started with Android are confused about entity. Why do you want to write entity classes? What's the use? What are you writing for?

For the understanding of the entity class when I get started is also confused for a long time, later with more slowly understand, this blog as a review and notes.

Writing Specification of entity (Entity Class) in Java

In everyday Java project development, entity (Entity Class) is essential, they generally have many properties, and corresponding setter and getter methods. The function of entity (Entity Class) is generally to map with data tables. So writing a speedy entity (Entity Class) is an essential skill in java development.

Writing entity class 1 in a project generally follows the following specifications:

1. According to your design, define a set of private attributes you need.

2. Based on these properties, create their setter and getter methods. (eclipse and other IDE can be generated automatically. How to generate it specifically? Please Baidu by yourself.)

3. Provide constructors with parameters and constructors without parameters.

4. Override the eauals () and hashcode () methods in the parent class. (These two functions are important if you need to compare two objects.)

Many understandings of Java entity classes:

A. is an attribute class, which is usually defined in the model layer

The B. 1-like entity class corresponds to a data table, in which the attributes correspond to the fields in the data table.

Benefits:

1. Encapsulation of object entity, embodying OO idea.

2. Attributes can judge and filter the definition and status of fields

3. After encapsulating the relevant information with an entity class, we can pass the entity class as parameters in the program, which is more convenient.

C. To put it bluntly, programmers don't have to write SQL statements when operating on databases

D. That is, one database table generates one class

This is convenient for database operation

Writing less code and improving efficiency can make programmers focus on logical relationships

E. Entity class is to write all the operations on a table in a class.

F. In the development of Java, it is often necessary to define some entity classes. The definition of these classes will directly affect the quality and difficulty of writing code.

Here are some experiences summarized by others.

1. The name of the entity class should correspond to the name of the table in the database as much as possible.

2. Entity classes should implement the java. io. Serializable interface.

3. Entity classes should have a parameterless constructor.

4. Entity classes should have a constructor with parameters (all parameters).

5. Entity classes have attributes and methods. Attributes correspond to the fields of tables in the database, mainly including getter and setter methods.

6. Entity classes should also have an attribute serialVersionUID. For example: private static final long serialVersionUID =-6125297654796395674L;

7. Attribute 1 is generally private type, method bit public type, and set method of attribute corresponding to ID field automatically generated by database should be private.

G. Entity classes are all instance objects. Instance objects open up a reference space for this object in the heap area of jvm, and let the reference point to an instance. Class declaration only opens up a reference for this object in the stack of jvm, and does not let the reference point at all.

For example:

1.String str;

2. String str = new String ("dgfgg");

The reference in 1 is only one, indicating that str should point to an instance of type String, but there is no specific reference to str and heap instances. That is, it does not point to an instance.

In 2, a reference (str) is defined and a specific reference is made to str, which points to the String instance from new later.

Constructors in Entity Classes + set Methods + get Methods:

Constructor: Initializes member variables

get, set methods, get and change the value of member variables, JavaBean specification only use get/set to access member variables

Constructor: Every time you write an Java file, you actually write a class (if you create a class, jvm will automatically open up a block of memory for this class). If you have a class, you need a class object. To generate a class object, you need a constructor to do one thing for the memory space you just applied for, such as assigning attribute values. Of course, if you don't, it defaults to a constructor of Class () {}, and of course it doesn't do anything.

What are your attributes? public? private? Default? Or protected? These four kinds of teaching materials must be understood and distinguished, which is very useful. If it is private, this attribute is not allowed to be changed and read by other objects, but sometimes it needs to be changed/read. What should I do? Write 1 public void setAbc (xxx) {} and 1 public xxx getAbc () {} to achieve the function of external reading and writing properties.

set, get are completely self-defined, which means setting initial values and getting values. You can also modify them into other characters. But it is better to use set, get, because you can understand the meaning of your code through words and make it easy to read.

Summary: Entity is the O/R Mapping mapping in Java, that is, a table in the database is mapped to a corresponding Java class, and there is also a mapping file. Given a more complex entity relationship (such as 1 to 1, 1 to many, many to many), you should skillfully write entity classes! !


Related articles: