Recently learned Java object-oriented knowledge points, has not had time to update the blog, because this piece of knowledge is really quite around the point. A knowledge point of a knowledge point out of the way, and for me this beginner to distinguish between the constructor and the method took a whole day of time. Now prepare to go through the knowledge point again.
First back down a concept:
Object-oriented programming is to put the data in the first place, and then consider how to manipulate the data.
Before we learned about Java's superiority, we learned that Java is a programming language for object-oriented design. So what exactly is object-oriented programming? To understand it, let's start with a quick look at another program design.
First, process-oriented programming:
Process-oriented programming is the execution of the top-down sequence and refinement; Its program structure is divided into several basic plates according to function, these plates form a tree structure; the relationships between the modules are as simple as possible and functionally independent; each plate is comprised of three basic structures of sequence, selection and circulation. The concrete method of its plate realization is to use with subroutine. The program flow is determined when the program is written.
(This concept is I am learning object-oriented in the online search, I put it and the concept of object-oriented programming in a blank sheet of paper, in fact, I do not know what is process-oriented programming, but, when I learned to object-oriented and class, and then come back to see this concept, there is a harvest. Did not learn to face the process is not afraid to speak, just learned the object-oriented more dare not comment. As I understand it, process-oriented is like making all the work flow of a thing reaching. and object-oriented, it is very smart, it is like a manufacturer of large-scale machinery, parts of what it does not necessarily have to do it, but it can be used to assemble (of course, you have to follow the rules of the others), the advantage is that you want to change that piece without worrying about the whole project, especially in the client that block. )
Second, object-oriented programming:
Object-oriented programming, called OOP, is the mainstream programming paradigm, which has replaced the "structured" process design and development technology for the 7 years of 20th century. Object-oriented is to put the data and the method of operation of the data together, as an interdependent whole, we call it object. The class is formed by abstracting its generality from similar objects. Most of the data in a class can only be handled by methods in this class. The class is connected to the outside world through a simple external interface, and the object communicates with the object through the message. The program flow is determined by the user in use. Java is completely object-oriented and must be familiar with OOP to be able to write Java programs.
(figure) illustrates the difference between object-oriented programming and process-oriented programming
The following sentence is important:
Object-oriented programs are made up of objects, each containing a specific part of the functionality exposed to the user and a hidden implementation part . (This sentence is really too important, after learning a single case, encapsulation, abstract class, interface, you can experience the!) )
Third, class
A class is a template or blueprint that constructs an object. The process of constructing an (construct) object from a class becomes an instance of the created Class (instance).
When studying here, there will inevitably be some panic, so many classes need to remember AH? Do you want to create a class yourself? Can you think of the contents of the class you created? All sorts of doubts in the heart lingering. In fact, all the code written in Java is in the interior of a class. The standard Java library provides thousands of classes that can be used for user interface design, date, calendar, and network programming. However, when we are making our own projects, we also need to add, delete and change them according to the specific circumstances.
Four, Package
Encapsulation (encapsulation, sometimes as data hiding), watching video lessons, they describe the package as a get, set method. Formally, encapsulation is simply a combination of data and behavior in a package, and the user of the object hides the way the data is implemented.
The data in an object is called the instance domain (instance field), and the process of manipulating the data is called method. There is a specific set of instance domain values for each particular class instance (object). The collection of these values is the current state of the object.
The following sentence is very important, back down:
The key to implementing encapsulation is that the methods in the class are absolutely not allowed to access the instance domains of other classes directly. A program interacts with the object's data only through its methods.
Encapsulation gives the object a ' black box ' feature, which is the key to improving reusability and reliability. This means that a class can completely change the way data is stored, as long as the same method is still used to manipulate the data, other objects will not know or mind the changes that have occurred.
What do you mean by this sentence? Which means if you have a need to define a Java class Someday, you don't have to worry about other classes being disturbed by you, you can extend a class to create a new class that you need. Don't forget, in fact, all the classes in Java originate from an object. Later, I will continue to introduce object.
The process of packaging = =:
1. Encapsulation is the privatization of attributes (private);
2. You need to provide Get/set method (Get/set not necessarily all come up, specifically to see your own needs. If you don't want others to get it, you don't provide get, and if you don't want someone to set it up, you refuse to provide set. )
= = = Accessor Permission:
| Access character |
In this class |
In this package |
In this bun class |
Outsourcing class |
Outer bun Type |
| Public (Common) |
can access |
can access |
can access |
can access |
can access |
| Protected |
can access |
can access |
can access |
Can not access |
can access |
| Default |
can access |
can access |
can access |
Can not access |
Can not access |
| Private (Common) |
can access |
Can not access |
Can not access |
Can not access |
Can not access |
code example:
1 Public classxiaonanhai{2 Private intAge ;3 Public voidSetage (intAge ) {4 if(age>=15&&age<=35)5 This. Age=age;}Else{6System.err.println "The age you entered is not within the specified range, please re-enter");7 }8}
Java Basics-Encapsulation