Encapsulation of Java Features

Source: Internet
Author: User

JavaThere are three main features: encapsulation, inheritance, polymorphism, which isJavaThe core of the language. In theJavaof learning, we must have heard "everything is the object", simply speaking is aJavaclass, someJavaclass is used to describe some of the properties of an object, such asSSH,SSMand some are used to interact with the page, such asservlet. In fact, these objects in the program can be understood as a basic module, these modules division of labor, different functions. Javais object-oriented, it is said that it is oriented to a separate module, inJavaprogram, in fact, and assemble the computer is the same, with that type ofCPU, video card, motherboard, go to the appropriate hardware module to choose on the line, simply say this isJavaobject-oriented. Encapsulation is equivalent to encapsulating the hardware module I said, the hardware module inside the details hidden, others do not know how the hardware to achieve the corresponding function.

Benefits of Encapsulation:

1. Can make the program more secure, after encapsulation, the external cannot directly access the corresponding module, must be accessed through the interface of the module.

2. Can reduce the coupling between the program, the purpose of the package is to make it a separate module, the mutual independent coupling between the modules must be reduced.

3. Hide the internal implementation details, through the access modifiers control the details of the class with the outside world isolation, so as to achieve the hidden data items and methods, and the only way to access these data items and methods is through the class itself, the class is eligible to invoke its own resources (methods, data item properties, and so on).

Packagecom.hulian.entity;

/**
* Created by Ethan on 2017/5/24.
 */
Public classCPU {
//Privatize Attributes
    PrivateIntegerTemp;
PrivateStringtype;

//access to the Open interface
     PublicInteger Gettemp () {
returnTemp;
}

Public voidSettemp (Integer temp) {
This.Temp= temp;
}

PublicString GetType () {
returntype;
}

Public voidSetType (String type) {
This.type= type;
}
}

This is a common package that will take the CPU 's temperature, and model privatization, provide external access to the interface, where you can more flexibly control the external access interfaces,the type of theCPU Is the factory, can be the type setter interface cancellation, only the getter interface reserved. We can also set the temperature range for the CPU:

Packagecom.hulian.entity;

/**
* Created by Ethan on 2017/5/24.
 */
Public classCPU {
//Privatize Attributes
    PrivateIntegerTemp;
PrivateStringtype;

//access to the Open interface
     PublicInteger Gettemp () {
returnTemp;
}

Public voidSettemp (Integer temp) {
if(temp>100| | TEMP<-50) {
System. out. println (the CPUcan't work ");
}
This.Temp= temp;
}

PublicString GetType () {
returntype;
}

Public voidSetType (String type) {
This.type= type;
}
}

we can see that we can control the properties precisely. Properties can also be converted, such as when setting the gender of the incoming short type of 0,1 to turn out the String type of men and women.

Encapsulation of Java Features

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.