Java Object-oriented encapsulation

Source: Internet
Author: User

Encapsulation is one of the three main features of Java object-oriented, usually we encapsulate the class through the package management mechanism, hide its internal implementation details, usually in the development does not allow the direct operation of the class member properties, so the property is generally set to private permissions, Classes generally give some public methods to allow the user to read or manipulate the data in a specific way, to avoid misoperation to ensure the security of the data.

The encapsulation step is generally divided into 3 steps, the first step is to modify the visibility of the property, set it to private; the second step is to create the Getter/setter method for getting/Setting the property value, which is used to read and write properties, and Eclipse also provides a menu that directly generates variable action methods. Select Source->generate Getters and Setters in the menu ... The system will automatically recognize the attributes, we can choose to create; the third step is to add property control to the Getter/setter method to control the legitimacy of the incoming parameters and ensure the data security.

A simple example is as follows:

1  Public classHello {2     Private floatScreen ;3     Private floatwidth;4     Private floatheight;5      Public floatGetscreen () {6         returnScreen ;7     }8      Public voidSetscreen (floatScreen ) {9Screen =Screen ;Ten     } One}

Then we read and write hello in the screen property is, can use our defined Getscreen and Setscreen for reading and writing, this simple implementation of the package

Of course we can also modify the required properties in other methods, but we use the Getter/setter method to implement read-write logic as a conventional programming habit

Access modifiers

The preceding access modifier, in addition to private and public, has protected, and can write nothing, and their permission relationship is as follows:

We generally use private for the attributes inside the class, which is the strongest protection, the public property is the most common, the most commonly used property is public and private, and the access modifier is usually at the front of the member.

this keyword

In the above example, the assignment statement in the Setscreen method, I use screen = screen, so that although there is no error, the compiler will give a warning because the method parameters and member property names, it is possible to cause confusion, In order to distinguish between the properties and parameters in this class we generally use the This keyword, that is, this.screen = screen; The method name in this class is called in addition to the direct write method, which is recommended for use with This.func (); This form calls methods in this class

Java Internal classes

A Java inner class is a class that is defined inside a class, and the class containing the inner class is called an outer class.

Inner classes can be better encapsulated, hiding inner classes in external classes, and not using the same package

The methods of the inner class can access all the properties of the external class directly, including the private

The inner class implements all the functions of the single class can be implemented, but sometimes have internal classes more convenient

Inner classes are divided into: Member inner class, Static inner class, method inner class, Anonymous inner class

1. member Inner class

The member inner class is the ordinary inner class, which can access all the members of the external class without worrying about any permission problems;

However, an external method cannot directly access properties or methods within a member's inner class, such as an external class name HelloWorld, an internal class name inner, and an external class method that accesses an inner class member by:

HelloWorld he = new HelloWorld ();

Inner in = He.new Inner ();

or directly with

Inner in = Helloworld.new Inner ();

The member inner class is then accessed through the in. Property/method, and when the program is compiled, 2 class files are generated, one generated by the inner class and one generated by the external class.

The external class compilation file name is: Helloworld.class Note the internal class compilation file name is: Helloworld$inner.class

In addition, when the inner class and the outer class have the same member variables and methods, the inner class first accesses the members of their class according to the nearest principle, and if you want to access the external class, you need to use this. Member to implement

2. Static internal class

A static inner class cannot directly access non-static members in an external class like a member inner class, and can be accessed through the new external class (). Member

Static inner class access to external class static members can be accessed directly using the member name, using the next sentence method to access

If a static member of an external class and an inner class member have the same name (note not just static members), you can access the internal members directly, and external static members access the class names. Members to access

When you create an object of a static inner class in an external class, you do not need to instantiate the external object as if the member's inner class, you can directly inner class object name = New Inner Class (), to create an object of the inner class

3. Method Inner Class

Method Inner class, that is, the definition of the inner class is in the method of the outer class, this is not considered too complex, the method inner class is only visible inside the method, so a sentence can only be used inside the method

Method internal use is very simple, directly with the inner class object name = New Inner Class (), you can access the inner class members.

Because the method inner class cannot be accessed anywhere other than the owning method, the method inner class cannot use the access control and the static keyword, which is simply written as follows:

1  Public classHello {2      Public voidShow () {3         //defines the method inner class without any modifiers4         classminner{5             //method Inner class member, need to add access modifier6             intA = 3;7              Public intGeta () {8                 returnA + 6;9             }Ten         } One         //To create a method inner class object AMinner mi =NewMinner (); -         //calling method Inner class method -         intScore =Mi.geta (); theSYSTEM.OUT.PRINTLN ("Call Result:" +score); -     } -      Public Static voidMain (string[] args) { -         //test method Inner class +Hello mo =NewHello (); - mo.show (); +     } A}

With this example, it is easy to see the use of the method's internal classes

4. Anonymous inner class

Anonymous inner class is an inner class without a name, can only be used once, usually coherent, to simplify the writing of the code, the anonymous inner class must inherit a parent class or implement an interface, followed by the Record object-oriented inheritance and polymorphism will be mentioned, here does not elaborate

Encapsulation basic knowledge is these, of course, there are many useful things worth refining, with new things to continue to summarize

Java Object-oriented encapsulation

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.