[Java entry notes] Object-Oriented Programming BASICS (I): classes and objects, java Object-Oriented Programming

Source: Internet
Author: User

[Java entry notes] Object-Oriented Programming BASICS (I): classes and objects, java Object-Oriented Programming
What Is Object-Oriented Programming?

Let's take a look at several concepts:

Process-oriented programming

Process-oriented is divided by sequential process based on the steps of development. process-oriented is actually the most practical way of thinking. It can be said that process-oriented is a basic method, it takes into account the actual implementation. The general process orientation is to improve step by step from top to bottom.

For example, if you want to eat, you need to: "buy food-wash food-cook-Eat" and so on. One-Step design is process-oriented.

Structured Program Design

Structured Program Design is a design concept that advocates analyzing system requirements by function. Its main principles can be summarized:

Top-down:When designing a program, you should first consider the overall, and then consider the details; first consider the global objectives, and then consider the local objectives. Do not pursue too many details from the beginning. First, design from the upper-level overall goal to gradually make the problem concrete.

Step by step:For complex problems, we should design some sub-goals as the transition and Gradually refine them.

Modular Design:A complex problem must be composed of a few simple problems. Modularization refers to the decomposition of the general goal to be solved by the program into sub-goals, and further decomposition into specific small goals. Each small goal is called a module.

Structured Program Design first uses the structured program analysis method for functional requirements analysis, then uses the structured design method to design the system, and finally uses the Structured Programming Method for system implementation.
This structured program design claims to Gradually refine the software by function, so this method is also calledFunctional-Oriented Programming.

In structured programming, the smallest program unit is a function. Each function is responsible for completing a function. The function as the program entry is called the main function, and the main function calls other functions, other functions call other functions to complete the functions of the entire program.

As shown in the figure, the top-down Design Method of Structured Program Design defines some functions as function modules and further refines the functions step by step. Structured Programming has two shortcomings:

1. The design is not intuitive enough. The model of the objective world needs to be divided into one function, and each function needs to process certain data.

2. poor scalability. When a function needs to be modified, the module structure needs to be modified from top to bottom.

Object-Oriented Programming

Object-oriented is the division of modules for a function. This part is similar to a structured program, but there is a difference, that is, objects have inheritance, polymorphism, and other forms, so that you do not have to re-write the program in the same place. It constructs a software system based on objective things in the real world and emphasizes thinking about problems with things in the real world (both objects.

When object-oriented development is used, the smallest program unit is a class. These classes can generate multiple objects in the system, and these objects directly reflect various objects in the objective world, such:

There is a class like "person". What features does it have (age, weight, etc.) and what features it has (eating, walking, etc.)? If we want to complete the process-oriented functions above, we only need to call the "food" method of a "person" object.

Three basic structures of a program

Regardless of the above design idea, the program design may involve the structure of the following three programs:

Sequential Structure

The sequential structure indicates that the operations in the program are executed in the order they appear.

Select Structure

Select structure indicates that a branch appears in the processing step of the program. It needs to select a branch for execution based on a specific condition. The selection structure can be single-choice, double-choice, or multiple-choice.

Loop Structure

Loop structure indicates that the program executes one or more operations repeatedly until a condition is false (or true. In the loop structure, the most important thing is: under what circumstances should a loop be executed? Which operations need to be executed cyclically? There are two basic forms of the Loop Structure: When Type Loop and until Type Loop.

Current cycle: Determines the condition first. When the given condition is met, the loop body is executed and the process at the loop terminal is automatically returned to the loop entry. If the condition is not met, then the exit loop body directly reaches the exit of the process. Because it is "execution loop when the condition is met", that is, it is first judged and then executed, so it is called a "When loop.

Cycle: Indicates that the loop body is executed directly at the structure entrance, and the condition is determined at the loop terminal. If the condition is not met, the loop body will be executed at the entrance until the exit of the loop reaches the exit of the process when the condition is true, it is executed first and then judged. Because it is "until the condition is true", it is called a type-until loop.

Class and Object

Object-Oriented Programming has two important concepts: class and Object ).

What is a class?

Class isAbstraction of a class of things with common characteristics in real lifeIs an abstract concept.

For example, cars, motorcycles, trucks, and buses all share the same characteristics, allowing them to walk and run people or items in a motorized manner. So we can abstract them into a class: car.

What is an object?

 Objects are a concrete embodiment of the abstract concept of classes.For example, there is a class called "Man", and the person named Zhang San and the person named Li Si are the concrete embodiment of the class "man", so they are objects. It should be noted that the object must be specific to an individual, and Michael Jacob is not necessarily an object, because there may be many people called Michael Jacob, and it must be "this Michael Jacob" is an object.

How to define a class

Definition Syntax:

[Modifier] class name {// class content}

 

For example:

public class Person{    }

 

We define a class called "Person.

Because the Java language is case-sensitive, you need to pay attention to the case-sensitive issue of keywords. Class names can be defined by yourself. It is recommended that names have actual meanings and the first letter of each word should be capitalized.

For a class, it contains three most common members: constructor, Field, and method. Each of the three Members can be defined as zero or multiple.

Constructor

The constructor is the fundamental way for a class to create objects. If a class does not have a constructor, objects cannot be created. Therefore, the system will define a constructor for each class by default, if it is not clearly defined, it is hidden. If it is clearly defined, it is not defined by default.

Syntax for defining constructors:

[Modifier] Class Name ([parameter name]) {
Function Code
}

 

The modifier can be public private protected or not written.

Example:

public class Person{    public Person(){}}

 

Field (attribute)

Field is used to define the status data of the class or the class instance, and describe the features of the object. The definition syntax is as follows:

[Modifier] Data Type attribute name = value;

 

Example:

public class Person{    public String name;    public int age;}

 

 

The Person class has two fileds used to describe the name and age respectively. In the above example, no constructor is defined for it, so the system will default a constructor for it: public Person (){}

Method

This method is used to define the behavior characteristics or function implementation of this class or this type of instance.

Syntax:

[Modifier] Return Value Method Name ([parameter name]) {// Function Code}

 

Example:

Public class Person {// defines a Person class public String name; // has a Field name (name), age (age) public int age; public Person () {}// the first constructor public Person (String n, int a) {// The second constructor. There are two parameters n and, if this constructor is called when an object is created, the name attribute is set to n and age is. Name = n; age = a;} public void eat (String s) {// method to simulate the eating function System. out. println (name + "eat" + s);} public void sleep (int hour) {// method to simulate the sleeping function System. out. println (name + "sleeping" + hour + "hours ");}}

 

 

Object creation and use

As mentioned above, the fundamental way to create an object is the constructor. You can call the constructor of this class through the new keyword to create an object, for example:

We have defined a "person" class above. We can use it to create an object.

Person p; // defines a Person-Type Variable p = new Person (); // returns a Person instance through the first constructor of the Person class with the new keyword, that is, the object.

 

You can also do this:

Person p = new Person ("Zhang San", 20); // we can simplify the preceding two steps as one step, call the second constructor, and pass two parameters, set the name and age for this object.

 

 

With the object, we can use it:

Use object: Use "." To assign a value to the class property: Object Name. Method of the class called by the property: Object Name. Method Name ()
System. out. println (p. name); // print the object name p. sleep (5); // call the sleeping Method

 

Example of creating and using objects in memory

The above code is as follows: Person p = new Person ("Zhang San", 20 );

This line of code creates a Person object, which is assigned to the p variable.

This line of code actually produces four things: p variable and Person object, "Zhang San" object, number 20

When there is an equal sign, the Code new Person ("Zhang San", 20) on the right of the equal sign will be executed first. This Code calls the public Person (String n, int a) constructor, however, we found that there are two variables in this method, String type n and int type a, so the memory is like this when executing this sentence:

Two variables s and a are generated. s is a reference type variable, which stores the memory address in a heap memory, this address points to an object of the String type in the heap memory. The Ah variable is an int type data, so its values are directly stored in the stack memory.

To call the constructor, an object of the Person type is created. The object has two attributes: name and age:

There are two codes in the constructor

Name = n; age =;

At this time, two values are assigned to the object.

Name = n: point to the same object

Age = a: copy the value to age

Continue execution. The constructor ends. Clear the and s variables, execute the code on the right of the equal sign of the statement, and create a Person Variable p. The variable points to the Person object returned by the constructor.

Object created.

 

 

Thank you for reading this article. If you have any mistakes, please correct them. (^_^)

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.