In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the basic concepts of JAVA object-oriented programming". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic concepts of JAVA object-oriented programming".
§6.1 programming for face-to-face objects
The programming method of the face-to-face process starts from every step of solving the problem, and it is suitable for simple problems with a smaller resolution ratio.
Ask a question. The C language uses the face-to-face process sequence to set up the model, but it is because there is almost no language supporting code reuse in C language.
The structure and the lack of a unified interface make it difficult for the programmer to control the complexity when the gauge of the sequence reaches a certain degree.
On the other hand, according to the special points of the real world, the complex things are managed according to the special points of the real world, and they are abstracted as pairs.
For example, with their own behavior and behavior, through the response to the elimination of interest to complete a fixed task.
6.1.1 Image, class and abatement
An opposite image is the set of variables and related square methods, in which the variables show the shape state of the opposite image, and the square method indicates the place of the opposite image.
With some behavior, the following table shows a characteristic of the object:
It can be seen from the diagram that a variable of an object forms the core of the object, and the square method surrounding its outer surface makes this.
Separate the elephant from its counterpart. For example, we can take the steam car as an object and use variables to show its current status.
Shape, such as speed, fuel quantity, type number, position, etc., its behavior can be acceleration, braking, gear shift and so on. I
When we operate a car, we don't have to worry about the details of how the parts in the car are operated, but only what the car can do.
In order to use the corresponding square method. In fact, the sequence of facing the object shows the sealing of the object, so that we don't have to
How does the behavior of the object come true? such details. Through the encapsulation of the object, the blocking and message hiding are realized.
Tibet is beneficial to the transplanting and integrity of the sequence, and to the management of complex objects at the same time.
Between elephants, it is necessary to make friends with each other in order to realize the complex behavior. For example, if you want to speed up a car, you must send it one.
Cancel the interest rate and tell it what kind of action (in this case is acceleration) in order to realize the parameters required for this action (this needs to be achieved.
Speed, etc.). The following table shows the cancellation transmission process between object An and object B.
It can be seen from the figure that a cancellation package contains the contents of three square faces:
Receiver of ● abatement
The square method for receiving and receiving objects in ●
The number of parameters required by the ● square.
At the same time, after adhering to the corresponding method, the object who receives and cancels the interest can return some information to the object who sends the message.
(as in the above example, the speed that has been reached will be shown on the meter of the car.)
Since the behavior of any object can be described by the square method, it can be fully realized through the information elimination machine system.
The intercourse between elephants, at the same time, in different places, and even between the objects of different hosts, can actually communicate with each other by passing and eliminating interest.
.
The object mentioned above is a physical thing, for example, each car is a different object. But it's multiple pairs.
Elephants often have some commonalities, such as all steam cars have wheels, directional discs, and often have some commonalities, such as all steam cars
All have wheels, directional wheels, brake installations, and so on. So we extract the commonness of a class of objects, which is called class. Definition in class
A class of variables and squares common to objects. To instantiate a class gives birth to an object of that class. Than if we can decide.
An automobile class is used to describe the commonness of all steam cars. Those who pass the class can realize the reuse of the code. We don't.
Use to describe each object (such as a car), but pass through a real example of a creative class (such as a car) to create a class of this class.
An object, which greatly reduces the design of the software.
6.1.2 succession
Through the object, class, we actually show the package, through the sub-category, we can inherit it.
For the above example, public cars, rental cars, freight cars and so on are all cars, but they are different cars, except for those with
Besides the commonness of automobiles, they also have their own special points (such as different operating methods, different ways of use, etc.). At this time, we can use
It is realized as a subclass of the automobile, and it inherits all the appearance and behavior of the parent class (steam car) and adds its own shape at the same time.
Behavior and behavior. Through the parent class and the subclass, we have realized the level of the class, which can be differentiated step by step from the most common class.
To define a subclass of a series At the same time, the passing inheritance also shows the code reuse, which increases the complexity of the program linearly, and
It is not a series of lengthening.
Multiple inheritance is supported in C++, that is, a class can inherit more than one parent class, which makes the reality of the object abnormal and complex.
And can not be expected (suppose that multiple parents have some similar variables and squares). On the other hand, Java only supports a single succession, which decreases greatly and lowers.
The complexity has been improved. Passing through the interface in Java can realize multiple inheritance, but the outline of the interface is simpler and more convenient to use.
It is limited to inheritance only, which enables multiple unrelated classes to have the same square method.
6.1.3 Polymorphism
Java implements polymorphism through square rewriting and square overloading.
Through square method rewriting, a class can have multiple squares with words of the same name, with different numbers passed to them.
And the parameters of the class type to decide which square method to use, this is polymorphism. For example, for a graphing class, it has a
The draw () square method is used to draw pictures or output text, and we can pass it a string, a rectangle, a circle, and so on.
It is also possible to re-specify the initial position of the drawing, the color of the figure, etc., for each kind of realization, it is only necessary to show a new
The draw () square method is fine without the need for a new name, which greatly simplifies the implementation and transfer of the square method, programmers and users.
You don't need to remember a lot of square names, you just need to pass in the corresponding parameters.
Through the overloading of the square method, the subclass can reproduce some of the methods of the parent class, so that it has its own characteristics. For example, for example
In the acceleration method of automobile category, some new parts can be added to its subcategory (such as racing cars) to improve the acceleration energy.
The acceleration method can be used to overload the parent class in the racing class. The overload hides the method of the parent class, so that the subclass has its own physical reality.
Go a step further to show the special characteristics of subclasses compared with their parents
In this section, we have explained some of the basic contents of face-to-face image sequence design, so let's not talk about it next.
How does Java really contain this?
§6.2 categories
Classes are the essentials that make up Java programs. It encapsulates the shape state and square method of a kind of object, which is the origin of this kind of object.
Type. In the examples in the previous chapters, we have defined some simple classes, such as the Hellowo rldApp class.
Public class HelloWorldApp {
Public static void main (String args []) {
System.out.println ("Hello World!")
}
}
As you can see, the implementation of a class consists of two parts:
ClassDeclaration {
ClassBody
}
Next, let's elaborate on each part in detail.
6.2.1 Class Sound
One of the simplest classes sounds like the following:
Class className {
……
}
For example:
Class Point {
……
}
At the same time, in the class sound, you can also include the parent class of the class, the interface implemented by the class, and the modifiers public, abstract, or
Final . We will introduce you in the following sections.
6.2.2 quasi-body
The class body defines the variables of the class and the square method supported by the class. The general constant variable is defined before the square method.
Must be asked), as shown below:
Class className {
MemberVariableDeclarations
MethodDeclarations
}
The following example defines a Point class, and clarifies its two variables x and y coordinates, and simultaneously realizes the init () square method to assign x and y.
Initial value.
Example 6.1
Class Ponit {
Int x,y
Void init (int ix, int iy) {
X=ix
Y=iy
}
}
The variables and squares defined in a class are members of the class. The member of the class can be limited by setting the right of interview.
There are several limits of authority for the interview of the elephant: private, protected, publi c, friendly. We will detail it in §6.6.
To discuss.
At the same time, for the members of the class, they can be divided into two types: the member and the member. We discuss it in detail in §6.8.
6.2.3 member variation
The voice of the simplest member variable is:
Type variableName
As in example 6.1, the variable of articulation, int x, is y
The class type of adult variable can therefore include simple class type, array, class and interface according to class type in Java. In one
Among the classes, the member variable should be unique, but the name of the member variable can be the same as that of a certain square method in the class, for example
Such as:
Class Point {
Int x,y
Int x () {
Return x
}
}
Among them, square method x () and variable x have the same name.
The member variable of the class is different from the local variable stated in the square method, and the scope of the member variable is the whole class.
While the scope of the local variable is only the inner part of the square method.
For a member variable, we can also limit its interview right (see §6.6) and use static to limit it as a class variable (see §
6.7), or subject to the following modifiers:
Final: used to articulate a constant, such as:
Class FinalVar {
Final int CONSTANT = 50
……
}
In the example, the constant CONSTANT is clearly stated and assigned a value of 50. For a constant limited by final, it cannot be changed in the program.
The value of. Capital letters are used for common measure names.
? (to be continued) 6.2.6 Constructor method
The construction method is a special method. Every class in Java has a constructor method that initializes one of the classes
A new target. The constructor method has the same name as the class name, and does not return any number of data type, in the reality of the constructor method.
At present, it can also be rewritten by the progressive method.
Example 6.5
Class point {
Int x,y
Point () {
Xx _ 0; y _ zero
}
Point (int x, int y) {
This.x=x; this.y=y
}
}
In the above example, we implement two constructors for class Point, both with the same name as the class Point. And we make
The square method is used to rewrite, and the x and y coordinates of the points are assigned different initial values according to different parameters.
Recall that in example 6.2, we used the init () square method to initialize the x and y coordinates of a point. The two complete the same function.
Well, what are the advantages of using the construction method?
When using the operator new to allocate memory for an object, the constructor method of the object is called, and when an object is created
You must allocate memory to it with new. Therefore, the initial avoidance is avoided by using the construction method, and it has to be adjusted every time after the formation of the pair.
Use the initialization square method of the object. If there is no real construction method, the Java operation system will automatically provide the default.
The construction method, which does not have any parameters.
In addition, the constructor method can only be called by the new operator. We will introduce it in detail in §6.3. The opposite constructor method is the same.
There is also a restriction on the right of interview (see §6.6).
6.2.7 finalize () square method
Before the object is collected, the Java operating system automatically invokes the finalize () square method of the object to interpret the unified funds.
Source, such as open text or socket. The voice of the law must be as follows:
Protected void finalize () throws throwable
The finalize () square method is implemented in class java.lang.Object. If you want to implement the method in a defined class for release
For the resources occupied by this class (that is, the finalize () square method of the parent class to be overloaded), after the translation of the resources used by this class, a
It is necessary to call the finalize () square method of the parent class to eliminate all sources of funding used by the object, including those obtained from the inherited customs system.
. The usual format should be:
Protected void finalize () throws throwable {
…… / / clean up code for this class
Super. Finalize ()
}
In this example, the finalize () square method of its parent class is called through super, which is related to super, which we will talk about in §6.4.
Java.lang.Ob-ject, we will also talk about it in §6.4.
§6.3 object
By giving an example of the class, we can create multiple objects, which can be transmitted to each other through the elimination of information.
The method of activating a specified object in order to change its state or let it produce a certain behavior), and finally complete the task of complex.
.
The life cycle of an object consists of three stages: generation, use, and elimination. Let's not talk about it next:
6.3.1 the formation of pairs of elephants
The formation of the image includes the internal content of the tripartite surface of the sound clear, the real example and the beginning. The usual format is:
Type objectName = new type ([paramlist])
1. Type objectName; articulates an object of class type type. Where type is grouped class type (including classes and interfaces).
The sound of the object does not allocate the memory space for the object.
two。 The operator new is an image allocation memory space, instantiating an object. New invokes the constructor of the object and returns the pair
A reference to the object (that is, the memory address where the object is located). Using new, you can instantiate multiple different pairs for a class.
Elephant. These objects occupy different memory spaces, so changing the shape of one of the objects will not affect the other objects.
The state of the state.
3. The last step in the formation of a pair of objects is to adhere to the method of construction and the beginning of the process. Because the constructor method can be rewritten in advance.
So the parameters that give different numbers or types will be divided into different constructors.
Taking the class Point defined in example 6.5 as an example, we generate the object of the class Point:
Point p1 = new Point ()
Point p2 = new Point (5p10)
Here, we generate two objects p1 and p2 for class Point, which use different constructor methods, and p1 call default.
The constructor method (that is, there are no parameters), and p2 calls the constructor method with parameters. P1 and p2 correspond to different memory spaces.
Their values are different, but they can be treated on their own.
Although the new operator returns the reference to an object, unlike the pointer in C and C++, the reference to the object refers to the
A middle data structure that stores the information of the related data type and the address of the heap where the object is located, while for
It is impossible to manipulate the memory address of the real world where the elephant is located, which ensures security.
6.3.2 the use of images
The usage of the object is enclosed by the member variable and square method of the object, which passes through the operator. Can be based on the reality of the variable interview and
The transfer of square method, variable and square method can permit or prohibit its counterpart by passing a set and fixed right limit of interview (see §6.6).
It's asking questions.
Let's first define a class Point, which adds some content to the definition of example 6.5.
Example 6.6
Class Point {
Int x,y
String name = "a point"
Point () {
X = 0
Y = 0
}
Point (int x, int y, String name) {
This.x = x
This.y = y
This.name = name
}
Int getX () {
Return x
}
Int getY () {
Return y
}
Void move (int newX, int newY) {
X = newX
Y = newY
}
Point newPoint (String name) {
Point newP = new Point (- x,-y, name)
Return newP
}
Boolean equal (int x, int y) {
If (this.x==x & & this.y==y)
Return true
Else
Return false
}
Void print () {
System.out.println (name+ ": X =" + x + "y =" + y)
}
}
Public class UsingObject {
Public static void main (String args []) {
Point p = new Point ()
P.print (); / / call method of an object
P.move (50,50)
System.out.println ("* * after moving *")
System.out.println ("Get x and y directly")
System.out.println ("x =" + p.xx + "y =" + p.y); / / access variabl
Es of an object
System.out.println ("or Get x and y by calling method")
System.out.println ("x =" + p.getY () + "y =" + p.getY ())
If (p.equal (505.50))
System.out.println ("I like this pointfully qualified employees!")
Else
System.out.println ("I hate it!")
P.newPoint (a new point) .print ()
New Point (10,15, "another new point") .print ()
}
}
The running result is:
C:java UsingObject
A point: X = 0 y = 0
* after moving *
Get x and y directly
X = 50 y = 50
Or Get x and y by calling method
X = 50 y = 50
I like this point!!!!
A new point: X =-50 y =-50
Another new point: X = 10 y = 15
1. Citing the variable of the object
To interview a variable of the object, the format is:
ObjectReference.variable
In which objectReference is a reference to an object, so it can be a generated object, or it can be paired.
It is like the table formula used by reference.
For example, we use Point p=new Point (); after we have created a Point-like object p, we can visit the x and y seats of the point by using p. X.
Standard, such as
P.X = 10; p.y = 20
Or use new to generate the citation of the object, and then answer questions directly, such as:
Tx = new point () .x
two。 The square method of calling the object
To call a square method of the object, the case is:
ObjectReference.methodName ([paramlist])
For example, if we want to move the object p of class Point, we can use
P.move (30pr 20)
Although we can change the coordinate of point p by directly interviewing the variables p.x and p.y of the object, it is the square that passes the square method.
The expression can better show the special point of facing the object, and it is suggested that the method can be transferred as much as possible.
In the same way, you can also use the reference of creating an object with new, and then directly call it, such as
New point (). Move (30pr 20)
As mentioned earlier, after the implementation of the square method of the object, it is common to return the value of the specified type, and we can use the method.
Using this value, for example, the square equal of class Point in example 6.6 returns a Boolean value, which we can use as a judgment bar to separate the line.
Different branches. Such as:
If (p.equal (200.30)) {
…… / / statements when equal
} else {
…… / / statements when unequal
}
In addition, the Point-like square newPoint returns the symmetric point at which the point is related to the origin, and the return value is also a Point type, we
You can inquire about its variable or call it, such as:
Px = p.newPoint () .x or
Px = p.newPoint (). GetX ()
Thank you for your reading, the above is the content of "what is the basic concept of JAVA object-oriented programming". After the study of this article, I believe you have a deeper understanding of what the basic concept of JAVA object-oriented programming is, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.