Getting Started with Java (6)--Object oriented approach

Source: Internet
Author: User

Classes and objects

The object-oriented idea is to simulate the real world.

Encapsulates the state data of something and its behavior, thus achieving ease of use, reuse, and the purpose of hiding internal state.

General variables, such as: int A, double b, etc.,

After we define it, it is a passive data, which is manipulated by other code.

The object, not only contains data, but also the method of manipulating the data, so that the data itself may be hidden.

The outside world only needs to invoke the object's method to achieve the purpose of manipulating the object.

This adds ease of use to the outside world, as these methods are specifically designed to avoid complexity and avoid misuse.

For the object, because the data is not directly manipulated, it achieves the purpose of protecting the internal data, and increases the security.

In this way, the object is like a more intelligent data than a normal variable.

An object is a composite type, or a complex type.

Because an object may contain more than one data to depict the current state of the object, there are several methods that describe the behavior that an object can make.

Define a normal data, int a = 100; Just fine, this type is simple.

If you need a 2nd, int b = ... I'll take care of it.

However, if you define an object directly, no matter how you design the syntax, to give more than one data, multiple methods will certainly be lengthy.

Just write, and then the boss again define 10 such objects, it will be tragic.

To avoid repetition and verbosity, a natural idea is to define a new "type" of your own, equal to int,

In this type, the data and methods that the object should contain are clearly described at once.

And then use this new type to define the object, is it clear and natural?

This new type is what we call---- class .

The following code defines the "point" on the plane, which contains x, y coordinates, and methods, such as the distance to the origin, distance.

1 classMyPoint2 {3     Private Doublex;4     Private Doubley;5     6      Public Doubledistance () {7         returnMATH.SQRT (x * x + y *y);8     }9      Public voidSetDoubleX1,Doubley1) {Tenx =X1; Oney =Y1; A     } -      Public DoubleGetx () { -         returnx; the     } -      Public Doublegety () { -         returny; -     } +}

The description of MyPoint in this code is relatively rudimentary, with only 2 data and 3 methods.

Data in front of the private decoration, that the outside world can not directly access the data, to achieve the purpose of protection.

But in this way, you have to provide some kind of access to X, Y, otherwise the data is worthless.

That's why the following set, Getx, gety methods are responsible for setting new values for x y, and for reading X y values.

Here's a look at how to use the new type of mypoint.

1  Public classA04112 {    3      Public Static voidMain (string[] args) {4MyPoint P1 =NewMyPoint ();5P1.set (10,20);6 System.out.println (P1.distance ());7 System.out.println (P1.getx ());8 System.out.println (P1.gety ());9     }Ten}

You might think that it would be better to just define X, and y more comfortably, by doing X in the same way.

It is true that object-oriented is indeed somewhat lost in efficiency, but in exchange for security. When software becomes larger and more complex, security and isolation are important.

For such a simple situation, of course, there is no advantage to see.

If you have to look at the advantages, so to think, for example, we need to limit the value of x y, the value of x y is limited to 1000 to 1000,

The value closest to the setting in the range.

At this point, we only need to modify the Set method, we do not need to change the other code, especially do not need to alarm the use of MyPoint code, which is a great advantage,

Because in large projects, MyPoint and the code that uses MyPoint are probably written by two different people, or even two different teams.

     Public void Set (doubledouble  y1) {        if(X1 < -1000) x1 = -1000;         if (X1 >  x1) = +;         if (Y1 < -1000) Y1 = -1000;         if (Y1 >  y1) = +;         = x1;         = y1;    }

This is also the meaning of "encapsulation".

The outside world can only invoke methods, provide the necessary parameters, and have no right to interfere with how the object is handled internally.

More theoretically, the outside world can only send requests to the object, as to how the object responds to the request, the outside world can not be left or right.

Outside: Please, just change your coordinates to 1200,900.

Object: No! 1200 Super range, I set it to 1000,900, love how to the ground.

Getting Started with Java (6)--Object oriented approach

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.