The concept of an object

Source: Internet
Author: User

What is an object?

An object is a program block that is related to state and behavior. The objects in the program are often used as virtual models of objects in the real world, and these objects are common things in our lives. This lesson will tell you how attributes and behaviors behave in objects, and introduce the concept of data encapsulation, and explain what is good for your program like this.

"Object" is the key to understanding object-oriented technology (object-oriented technology). Now look around you will find many objects in the real world: your dog, your desk, your TV set and your bike.

Objects in the real world have two attributes: they all have state and behavior (behavior). Dogs have their own state (name, color variety, hunger), have their own behavior (hair removal, fetching things, wagging the tail). Bicycles also have their own status (whether the gears are flexible, the tempo of the pedals, the current speed) and their behavior (replacing gears, replacing pedals, braking). The best way to follow object-oriented design programs is to determine the state and behavior of objects in the real world.

Now take a few minutes to look at the objects in the real world and they are all around you. For each object you see, ask yourself two questions: "What is the possible state of being in this object?" "and" What kind of behavior can this object have? ”。 Be sure and write down what you have observed. As you have done, you will find out how complex objects in the real world are, and the lights on your desk may have two states (open, closed), there may be two actions (open, close), but the radio on your desk may have another state (open, closed, current volume, current configuration) and behavior (open, close , increase volume, lower volume, find, scan, tone. You may also notice that such an object, in turn, will include that object. These observable things in the real world can be introduced into the object-oriented world.

An object in a program

The object in the program is the same as the concept of the object in the real world: it is composed of state and related behavior. Objects store state by field fields (variables in program) and expose behavior by means of methods (functions in programs). Method controls the intrinsic state of the object and establishes a connection between the objects through its own proprietary mechanism. Hides the inner state and the way through the object, these methods are encapsulated and can complete all the interactions. Data encapsulation is the basic idea of object-oriented programming.

Consider the bike, for example:

A bicycle is imagined as an object of the program

By the state (current speed, current pedal frequency and current gear) and the method provided in order to change the state, it can be allowed to control the object besides. For example, if a bicycle has 6 gears, one or six gears may be discarded by a change of gears.

There are several benefits to adding code to private objects:

1. Modular Nature:

The code for one object does not depend on other object code. Once created, the object is easily delivered to the system.

2. Information concealment:

By combining with the object method, the inner detailed execution information will not be revealed outside.

3. Code reusability:

If an object is present (perhaps written by another software developer), you can use it in your project. This allows experts to implement, test, and debug complex and specific objects that you can add to your code as long as you think it works.

4. Easy to plug in and debug:

If there is a problem with an object, you can easily remove it from your application, and you can replace it with other objects. This is similar to the problem of identifying machines in the present world. If the latch is broken, you replace it instead of the whole machine.

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.