Java Object-oriented Note 2

Source: Internet
Author: User

Let's take a look at the methods in object-oriented languages, when defining a method, it is particularly similar to a function in a structured programming language and deserves attention


Is that the methods in Java cannot exist independently, and all methods are defined in the class.


What is the difference between the method and the function we are talking about?


In the Structured programming language, the whole software is composed of functions, and in object-oriented programming language, the whole software is set by one class


Yes. So when defining a method, you can only define it in a class, and if you use static to decorate it, then this method belongs to this class, otherwise this method belongs to this


Instances of a class.


Usually when we call another method in one method of the same class, if the method is the normal method, the default is to use this as the caller, if the


static method when the method is dropped, the class is used by default as the caller.


If more than one method with the same name is defined in the same class, and the formal parameter list is different, then we can call this method overloaded.


Method overloads follow two identical differences: the same method name in the same class, and the parameter list is different.


In the Java language, variables can be divided into two main categories depending on where they are defined: member variables and local variables.


The member variables are divided into class field and instance field two, which is the instance field when the field is defined without static, and the class is decorated with static


field, where the class field is a member of the class itself, and the instance field is a member of the instance.


The local variables are divided into the following three types according to the defined form:


1. Formal parameter: The variable defined when defining the method signature, the scope of the formal parameter is valid throughout the method.


2. Method local variable: a local variable defined in the body of a method, scoped from where the variable is defined, to expire at the end of the method.


3. code block local variable: a local variable defined in a code block, the scope of the local variable takes effect from where the variable is defined, to the code block


When the bundle is deactivated.


Local variables, except for formal parameters, must be explicitly initialized for both method local variables and code local variables.


Encapsulation is one of the three main characteristics of object-oriented. Refers to hiding the state information of an object inside an object, without allowing external programs to directly access the object's internal


The method provided by the class to operate and access internal information. We can achieve several purposes through encapsulation:


1, the implementation of the hidden class details.


2. Allow users to access data only through predetermined methods, thereby adding logical control to the method and restricting unreasonable access to the field.


3, the data can be checked to ensure the integrity of the object information.


4, easy to modify, improve the maintainability of the code.


Therefore, to achieve a good package needs to be considered in the following two aspects:


1, the object's field and implementation details are hidden, not allow external direct access.


2, the method exposed, let the method to control the field for safe access and operation.


The above two aspects need to be implemented by using the access controls provided by Java.


From the previous section we know that Java provides three access controls: private, protected, and public, representing 3 levels of access control, plus


There is also an access control level that does not add any access control. The control levels for these access controls, such as top to bottom, represent access control


The system level is from small to large:

The above four access control levels are described in more detail below:


Private: If a member of a class is decorated with the private access control, the member can be accessed only within the current class.


Default: If a member of a class or an external class does not use any access control modifiers, it is called a packet access, and the default access control


or external classes can be accessed by other classes under the same package.


Protected: If a member of a class uses the protected access control modifier, the member can be accessed either by another class in the same package, or


can be accessed by subclasses in different packages, and if you use protected to decorate a method, you usually want its subclasses to override this method.


Public: This is the most permissive level of access control, if a member or an external class is decorated with the public access control, the member or


An external class can be accessed by all classes, regardless of whether the access class and the accessed class are in a package, and whether they have a parent-child inheritance relationship.




Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43852883 Emotional Control _  








Java Object-oriented Note 2

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.