Introduction to Java programming tutorial--JavaBean specification

JavaBean

       JavaBean is a program paragraph designed to be reusable, and these paragraphs do not only serve a certain program.

       Each JavaBean has a specific function, and the corresponding JavaBean can be called when this function is needed . In this sense, JavaBean greatly simplifies the program design process and facilitates the reuse of other programs.

       Therefore, JavaBean is a Java class that conforms to certain specifications .

JavaBean classification

        JavaBean is divided into business bean class and entity bean class.

        The main function of the entity bean is to encapsulate the data to be used in the system. These data include the user's input from the front desk and the data read from the database. It is a relatively simple type of JavaBean .

Javabean specification

1.  The JavaBean class must be a public class, and its access attribute is set to public .

2.  The JavaBean class must have an empty constructor.

3.  All member variables of the JavaBean class are decorated with private . And provide public set and get methods (for assignment and retrieval) for each member variable.

4.  Member variables should be accessed through a set of accessor methods ( getXxx and setXxx ): For each attribute, there should be a dedicated instance variable with matching public get and set methods. For Boolean values, isXxx can be used instead of getXxx .

5.  Method naming rules: If the variable is abc , the corresponding methods are setAbc () and getAbc () .

object-oriented encapsulation

       The Javabean specification embodies object-oriented encapsulation.

       Object-oriented encapsulation refers to the use of abstract data types, such as classes ( class ), to encapsulate data and data-based operations to form an indivisible independent entity. In this way, data (private variables) are protected inside the class to hide relevant details as much as possible, and only some external interfaces (public methods) are kept in contact with the outside.

       Usually, a class contains two types of data: public data ( public type) and private data ( private type). Public data allows direct access to the external environment, while private data does not allow direct contact with the external environment. It can only be accessed and modified by methods of this class.


        As shown in the figure, class B can directly access the public data of class A, but cannot directly access the private data in class A. It can only access the public methods in class A first, and then use this method to access the private data in class A. This processing mechanism allows some public information (public data) in the class to be opened to the outside world, while preventing data with strong confidentiality (private data) from being exposed to the outside world.
 

 

Guess you like

Origin blog.csdn.net/u010764893/article/details/131122959
Recommended