Java programmer must read: Basic (2)

Source: Internet
Author: User
Tags object key variables variable access
Program | programmer Java programmer must read: Basic articlesTime: 2001/09/13 13:31 Author: ZSC Pacific Network College

 
Two. Object-oriented Programming


If you've never used an object-oriented language before, you'll need to understand the concept before you start writing Java code. You need to understand what objects are, what classes are, how objects and classes are related, and how messages are communicated between objects. The previous section of this tutorial will describe the concept of object-oriented programming, and the following tutorial will teach you how to code this concept.


2.1 What is an object

An object is a set of software for related variables and methods. Software objects are often used to mimic some objects around us in the real world. object is the key to understanding object-oriented technology. Before you study, you can look at real-life objects, such as dogs, tables, televisions, bicycles, and so on. You can see that objects in the real world have two common characteristics: they all have states and behaviors. Dogs, for example, have their own state (name, color, fertility, starvation, etc.) and behavior (such as wagging the tail, etc.). The same bike also has its own status (such as current gear, two wheels, etc.) and behavior (such as braking, acceleration, deceleration, change of position, etc.).

The software object is actually the shape of the real world object, because it has the same state and behavior. A software object uses one or more variables to maintain its state. A variable is a data item that is named by a user identifier. A software object uses its methods to perform its behavior. Method is a function (subroutine) associated with an object.

You can use software objects to represent objects in the real world. You might want to use an animated program to represent a real-world dog, or a bike that can be used to control an electronic bike to represent the real world. Similarly, you can use software objects to sculpt abstract concepts, such as an event that is a common object used in a GUI window system, which can represent a user's response to a mouse button or a key on the keyboard.

Figure 1 is a public visual representation of a software object.


(Figure 1)

The state and behavior of the software object can be expressed in variables and methods in the object. The software object of a real-world bicycle is to have a variable that indicates the current state of the bicycle: 20mph, its current position is third gear. These variables are known as instance variables because they contain states for special bike objects, and in object-oriented technology, special objects are called instances.

As shown in Figure 2, is the bicycle styling as a software object.



(Figure 2)

In addition to variables, software bikes also have the means to brake, change the pedal pace, and change the gears. These methods are known as instance methods because they check or change the status of special bike instances.

The object graph above shows that the object's variables form the center of the circle. The method is located around the variable and hides the core of the object from other objects in the program. The method of protecting methods to wrap an object's variables is called encapsulation. This object graph is the representation of object ideal and the final goal of object-oriented system designers. But this is not the whole story. Usually, for some practical reason, an object might expose some of its variables or hide some methods. In the Java programming language, an object can specify one of four types of access levels for variables and methods. This access level determines which objects and classes can access variables or methods. Accessing variables and methods in Java can be converted to control access to the member functions of the class. Encapsulation of related variables and methods to a concise software set is a simple and powerful way to provide software developers with two main benefits:

Modularity: The source code of an object can be written and maintained independently of other object sources. Similarly, objects can be easily passed through the system. You can give your bike object to other objects and it will still work properly.

Information hiding: If an object has a common interface, then other objects can communicate with it. This object can maintain private information and methods that can be changed at any time without affecting other objects that are resistant to it. So you don't have to understand the mechanics of gears in a bicycle to use it.

Related Article

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.