Java10-java grammatical Basis (ix)--java encapsulation

Source: Internet
Author: User

Java10-java grammatical Basis (ix)--java encapsulation

First, the three main features of Java: Encapsulation, polymorphism, inheritance

Encapsulation: Encapsulates the data members and member methods of an object through a class, guaranteeing that only trusted classes or objects can access these methods and data members, and that information is hidden from untrusted classes or objects.

Inheritance: Inheritance derives new classes from existing classes, and new classes can absorb data members and methods of existing classes, and can extend new capabilities.

Polymorphism: Polymorphism refers to different objects, and receiving the same message can produce different results.

Second, the encapsulation of object-oriented programming

Implementation of encapsulation: through the private keyword

1. Encapsulating data members and Member methods

(1) Encapsulating data members: In general, there is no special description, the data members of the class are private

Private data type data member name;

(2) Encapsulating member methods: In general, there is no special description, the member method of the class is not private, otherwise there is no way to access the class

Private method return value method name (parameter list) {}

Private class name (parameter) {}--Encapsulation construction method

Third, access control modifiers

1, Private: Privately, can only be accessed within the class;

2, Protected: protection, in the class of the inner, sub-class, the same package;

2.5, the default: In the class outside, subclass, the same package;

3. Public: Publicly available, accessible anywhere.

Note: There can be only one common main class in any Java file: The public class class name () {}, and then the other class cannot be used to add any other classes, you can write class Test () {}

Iv. Setter and Getter methods

How do I access private data members outside of a class?

Access: Public String getName () {return name;}

Modified: public void SetName (String name) {this.name = name;}

Note:

(1) Eclipse automatically generates get and set methods: Right-click--source--generate Getters and Setters ...

(2) A field is a data member (member variable)

Iv. getters and Setters methods: Provides a way for users to access private data members of classes outside the class

1. Setters: Set the value of the private data member

Format: public void set Private data member name (private data member type name to be modified to private member name) {Private data member name = Private data member name to be modified;}

Public void setName (String name) {

this. Name = name;

}

2. Getters: Gets the value of the data member

Format: Public String GetName () {return private data member name;}

Public String GetName () {

return name;

}

Five, this keyword--represents the current object

Role:

1. Data members and member methods that refer to this class:

this. data member name

This. Member method name ([parameter])

2, call the construction method of this class

This. Constructor method name ([parameter])

Note: In a constructor, if you call another constructor method with this, it must be the first statement in the constructor method;

Question: Super () and this () Why must you be on the first line?

The reason for super () in the first line is that the subclass may have access to the parent class object, such as using the parent class object's member functions and variables in the constructor, using the parent class in the member initialization, using the parent class in the code block, and so on to ensure that the parent class object is initialized before the child class can access it.

The reason for this () in the first line is: to guarantee the uniqueness of the initialization of the parent class object. We assume that Class B is a subclass of Class A, and what happens if this () can be used on any row of the constructor? First the program runs to the first line of constructor B (), finds that no call to this () and super (), automatically completes super () on the first line, finishes initializing the parent class object, and then returns the constructor of the subclass to continue execution when running to the "this (2)" of constructor B (); , the B (int) constructor of the Class B object is called, and the parent class object is initialized again in B (int)! This creates a waste of resources, of course, it can also cause some unexpected results, anyway, it is unreasonable, so this () can not be present in addition to the first row of other rows!

3, the use of this case

(1) When the formal parameter name and the class data member name are the same;

(2) When the local variable name of the method has the same name as the variable of the class;

(3) In one construction method, when another constructor method of the class is called, that is, the current object.

Java10-java grammatical Basis (ix)--java 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.