I guess many friends who are getting started with Android are confused about entity, why do you need to write a physical class? What's the use? What to write for?
When I started with the understanding of entity classes, I was confused for a long time. I used it more and then slowly understood it. This blog is just for review and notes.
Writing specifications for entity (entity class) in Java
In daily Java project development, entity (entity class) is essential. They generally have many attributes and have corresponding setters and getter methods. The function of entity (entity class) is generally to map with data tables. Therefore, writing out a standard entity (entity class) quickly is an indispensable skill in Java development.
Writing entity classes in projects generally follows the following specifications:
1. Define a set of private attributes you need according to your design.
2. Create their setter and getter methods based on these properties. (IDEs such as eclipse can be automatically generated. How to generate them? Please Baidu on your own.)
3. Provide constructors with parameters and constructors without parameters.
4. Rewrite the eauals() method and hashcode() method in the parent class. (These two functions are important if you need to involve comparisons between two objects.)
Many understandings of Java entity classes:
A. is the attribute class, usually defined in the model layer
B. A general entity class corresponds to a data table, and its properties correspond to fields in the data table.
benefit:
1. Encapsulation of object entities reflects OO ideas.
2. Attributes can judge and filter field definitions and status
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, it is to allow programmers to not write SQL statements when operating the database.
D. It is a database table that generates a class
This makes it easier to operate the database
Writing less code and improving efficiency can make programmers focus on logical relationships
E. An entity class writes all operations on a certain table in one class.
F. In Java development, some entity classes are often defined. The quality and difficulty of writing code are directly affected.
The following are some experiences summarized by others.
1. The name of the entity class should be the same as the name of the database table.
2. Entity classes should implement interfaces.
3. Entity classes should have a constructor without parameters.
4. The entity class should have a constructor with parameters (all parameters).
5. Entity classes have attributes and methods. The attributes correspond to the fields of the table in the database, mainly including getter and setter methods.
6. The entity class should also have a property serialVersionUID. For example: private static final long serialVersionUID = -6125297654796395674L;
7. The attribute is generally private type, and the method bit is public type. The set method for the attribute corresponding to the ID field automatically generated by the database should be private.
G. All entity classes are instance objects. The instance objects open up a reference space for this object in the heap area of jvm, and let the reference point to an instance. The class declaration only opens a reference to this object in the stack of jvm, and does not allow the reference to any pointing.
For example :
str;
str = new String (“dgfgg”);
The 1 is just a reference, indicating that str should point to an instance of type String, but has not specified the instance of str and heap area. That is to say, it has not pointed to an instance yet.
The 2 defines a reference (str) and points to str, and the content it points to is the String instance that comes out after new.
Constructors + set method + get method in entity class:
Constructor: Initialize member variables
get, set method, get and change the value of member variables. The JavaBean specification stipulates that only use get/set to access member variables
Constructor: Every time you write a Java file, you actually write a class (create a class, and jvm will automatically open up a piece of memory space for this class). With a class, you need to have class objects. To generate class objects, you need to do something about the memory space you just applied for, assign attribute values and other work. Of course, if you don't write it, it will default to a Class(){} constructor, of course it does nothing.
What are your attributes? public?private? default? Or protected? If you find these four types, you must understand and distinguish them clearly. They are very useful. If it is private, this property is not allowed to be modified or read by other objects, but sometimes it needs to be modified/read. What should I do? Writing a public void setAbc(xxx){} and a public xxx getAbc(){} will realize the function of external read and write attributes.
set, get is completely self-determination, which means setting the initial value and obtaining the value. You can also modify it to other characters. But using set is better, because you can understand the meaning of the code you wrote through words, which is easy to read.
Summarize:Entity is the O/R Mapping mapping in Java, that is, a table in the database is mapped into a corresponding Java class, and there is also a mapping file. Given a more complex entity relationship (such as one-to-one, one-to-many, many-to-many), you should skillfully write the entity class!!
The above detailed explanation of the role of entity entity in the Android project is all the content I share with you. I hope you can give you a reference and I hope you can support me more.