Java analysis One of three major features package

Source: Internet
Author: User
Tags modifier

Before we talk about Java three features, let's look at what is object-oriented and why Java is an object-oriented language.
Object-oriented is a kind of programming idea which is different from process oriented. We can use this example to look at the difference between object-oriented and process-oriented by using an elephant in a refrigerator.
Presumably through this example, the distinction between object-oriented and process-oriented should be clear. Process-oriented refers to our focus on each stage of the process, and object-oriented refers to our main focus on the performers of each action. Java is an action-oriented performer, focused on writing classes, focusing on the language of code and function reuse.

Packaging

As we have already said, Java is focused on the writing of classes, so encapsulation is naturally a wrapper for classes. There are many benefits to class encapsulation.

    1. The internal implementation of the method can be completely hidden, providing only one method of invocation to others, so that other people who use the class do not need to be concerned about how to implement it, just know how to invoke it.
    2. The benefit of hiding the internal implementation of a method allows you to arbitrarily modify the structure of a class without affecting other people's running results while preserving the calling method unchanged.
    3. Encapsulation also separates the properties of the class, dividing the properties of the class into private and public properties. Private properties are only called by the class itself, and public properties provide only one method for external invocation.
    4. In accordance with the software terminology, a good encapsulation is able to reduce coupling.

How to encapsulate a class needs to be based on the objective attributes of the class itself and the actual needs.
Like a MyTime class.

public class MyTime{    public String date;    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 date= df.format(new Date()); public String getDate() { return date; }//// public void setDate(String date) {// this.date = date;// }}

It is obvious that this class provides only the time to display the system, and does not provide a way to modify the system time externally. This is also based on the objective nature of the class itself, time is inherently not artificially changed properties, of course, and so on, there are many. We are simply giving an example of how we can design an external access method for our class.

Access rights

In the above code, we have seen that date can be accessed by calling the GetData () method, but the modifier of date is public, in other words, we can modify and read the date property entirely by a new MyTime object, which violates the original purpose of our encapsulation. As a result, Java introduces different access permissions to qualify the event. The following access rights are explained in "Java Programming ideas"
Public: Interface access rights. That is, public-decorated member methods, properties are accessible anywhere.
Private: You can't access it. Also say private modified member methods, properties are only accessible in this class, outside of this class, you are unable to access the contents of this class, this is very useful, our example code above, because it is public decoration, so there will be a problem of encapsulation is not complete, If our member variable date is decorated with private, then the above problem will not occur.
Protected: Inherit access rights. Before we talk about inheriting access, we think of a problem where we have a base class that needs to be inherited. The member property in the base class, which we want to be accessed by the inherited class, we can set it to public, but once that happens, other classes will be accessed, in addition to the inherited class. This is obviously not what we expected, so Java introduced the protected modifier to indicate that the part being modified is accessible to the derived class, and that he can be accessed by other classes within the same package.
So, to summarize, if we want the methods and properties of the class to be accessed by all classes, use public, if only the classes that can be inherited and the same package are accessed using protected, if you only want to access in the current class, you can not be accessed by any other class will use private.

Summarize

After understanding the specific meaning and benefits of encapsulation, we can use the above access rights to encapsulate what we want to encapsulate, to hide specific implementations through access rights, and to provide interfaces for external access.

PublicClass Student {PrivateString name;PrivateString age;PrivateString Handlename (String name) {Return "I ' m" + name; private string handleage ( String age) {return ages +  "old";} public string getName () {return Name } public void setName (String Name) {this.name = handlename (name);} public string getage () {return Age } public void setage (String Age {this.age = Handleage (age);}}         

In the example above, we have a simple encapsulation of the student class, first we cut off the possibility of external direct calls to Name,age, and through the set and get methods to provide access to the Name,age method, while the above example is also reflected, In the case of the external access method unchanged, we modify the internal implementation at will, we first write the method of handle only for the class internal call, and use the handle method to modify the display result of the name,age. This ensures that the method of modifying the display results can only be invoked on its own, and that the internal implementation is modified as required in the event of external access unchanged. This is also a simple example of encapsulation. The example of its invocation is as follows: The student object can only invoke the following content, and other content that we write ourselves cannot be called.

public class Test {    public static void main(String[] args) { Student student = new Student(); student.setName("byhieg"); student.setAge("23"); System.out.println(student.getName() + " " + student.getAge()); }}

Java analysis One of three major features package

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.