Home > Java > javaTutorial > Java Basics - Encapsulation

Java Basics - Encapsulation

巴扎黑
Release: 2017-06-26 11:26:42
Original
1454 people have browsed it

I have recently been learning Java object-oriented knowledge points, and I have not had time to update the blog, because this knowledge point is really confusing. Knowledge points emerged one after another, and for me, a beginner, it took me a whole day to distinguish between constructors and methods. Now I’m going to go over the knowledge points again.

First memorize a concept:

Object-oriented programming puts data first, and then considers the method of operating data.

Before, when we were learning the advantages of Java, we learned that Java is an object-oriented programming language. So what exactly is object-oriented programming? To understand it, first let's take a brief look at another kind of programming.

1. Process-oriented programming:

Process-oriented programming is executed sequentially from top to bottom and gradually refined; its program structure is divided into several functions according to functions. Basic blocks, these blocks form a tree structure; the relationship between each module is as simple as possible and relatively independent in function; each block is composed of three basic structures: sequence, selection and circulation; its blocks The specific way to achieve this is to use subroutines. The program flow is determined when the program is written.

(I found this concept on the Internet when I was learning object-oriented programming. I copied it and the concept of object-oriented programming on a piece of white paper. In fact, I didn’t know what process-oriented programming was. , however, when I came back to look at this concept after learning object-oriented and classes, it was fruitful. I didn’t dare to say much if I had never learned process-oriented, and I didn’t dare to comment much if I had only learned object-oriented. According to my understanding, process-oriented is like making all the workflow of something interlocking, and one step affects the whole body. As for object-oriented, it is very smart, it is like a manufacturer of large machinery. Manufacturers may not necessarily make all the parts themselves, but they can try their best to assemble them (of course, you have to follow the rules set by others when assembling them). The advantage of this is that you don’t have to worry about the whole project being affected if you want to change that part. Implicated, especially on the client side)

2. Object-oriented programming:

Object-oriented programming is referred to as OOP, which is today’s mainstream programming. paradigm, which has replaced the "structured" procedural programming development techniques of the 1970s. Object-oriented is to put data and methods of operating data together. As an interdependent whole, we call it an object. A class is formed by abstracting the common features of similar objects. Most data in a class can only be processed by methods of this class. The class relates to the outside world through a simple external interface, and objects communicate through messages. The program flow is determined by the user. Java is completely object-oriented, and you must be familiar with OOP to write Java programs.

(Picture) Demonstrates the difference between object-oriented programming and process-oriented programming

The following sentence is very important:

Object-oriented programs are composed of objects, each object contains a specific functional part that is exposed to the user and a hidden implementation part. (This sentence is really important. You will be able to understand it when you learn singletons, encapsulation, abstract classes, and interfaces in the future!)

3. Class

A class is a template or blueprint for constructing objects. The process of constructing an object from a class becomes creating an instance of the class.

When I study here, I will inevitably feel a little panicked. There are so many categories that need to be memorized? Do you want to create a class yourself? Can the contents of the classes you create be comprehensive? All kinds of doubts linger in my heart. In fact, most of the code written in Java is located inside a class. The standard Java library provides thousands of classes for user interface design, dates, calendars, and network programming. Even so, when we are working on our own projects, we also need to add, delete and change according to different specific situations.

4. Encapsulation

Encapsulation (sometimes called data hiding), watching the video class, they describe encapsulation as the get and set methods. Formally, encapsulation is nothing more than combining data and behavior in a package and hiding the implementation of the data from users of the object.

The data in the object is called the instance field, and the process of operating the data is called a method. For each specific class instance (object) there is a specific set of instance field values. The collection of these values ​​is the current state of the object.

The following sentence is very important, memorize it:

The key to achieving encapsulation is that methods in a class must not directly access the instance fields of other classes. A program interacts with an object's data only through its methods.

Encapsulation gives objects 'black box' characteristics, which is key to improving reusability and reliability. This means that a class can completely change the way it stores data, as long as the same method is still used to manipulate the data, other objects You won't know or care about the changes.

// What does this mean? That is to say, if you need to define a Java class one day, you don't have to worry about other classes being affected. You can create a new class you need by extending a class. Don't forget, in fact, all classes in Java are derived from an Object. Later, I will continue to introduce Object.

#====The process of encapsulation:

1. Encapsulation is to privatize attributes;

2. You have to provide get/set methods (get/set does not It must be mentioned, depending on your own needs. If you don't want others to get it, don't provide get; if you don't want others to set it, you refuse to provide the set access code)

====. Permissions:

##private (commonly used)AccessibleNot accessibleNot accessibleNo AccessibleNot accessible## Code example:
Access code In this class this In the package In the subcategory of this package Outsourcing category Outsourcing subcategory
public (commonly used) can be accessed can be accessed can be accessed can be accessed can be accessed
protected can be accessed can be accessed can be accessed Not accessible Can be accessed
Default can be accessed can be accessed can be accessed Not accessible Not accessible
1 public class XiaoNanHai{2 private int age;3 public void setAge(int age){4 if(age>=15&&age<=35)5 this.age=age;}else{6 System.err.println"你输入的年龄不在指定范围内,请重新输入”);7 }8 }
Copy after login

The above is the detailed content of Java Basics - Encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template