Java basic syntax-object-oriented

Source: Internet
Author: User

OneObject Oriented1.1 What is process-oriented, object-oriented

process-oriented and object-oriented is a way of thinking in our programming, writing programs.

The process-oriented programming approach is the process of thinking "What should I do" when encountering one thing, and then implementing it step-by-step.

object-oriented programming, is to meet one thing, think "I should let who do", and then that "who" is the object, he will do this thing is his own thing, anyway, the last group of objects together can be good on the line.

1.2the benefits of an object-oriented approach to thinking

After the use of object-oriented analysis through real-life scenarios, we begin to analyze the process-oriented and object-oriented differences to make a summary:

Object-oriented Thinking mode is a kind of thought which is more accord with people's thinking habit.

The process-oriented way of thinking is more of the embodiment of the executor (self-doing things), object-oriented more embodiment is the conductor (directing objects to do things).

The object-oriented way of thinking simplifies complex problems.

TwoClasses and Objects2.1The embodiment of the object in the Code

  In the analysis of real-life things, it is found that these things have their specific characteristics and functions, these features and functions constitute this special thing.

describe a car.

Analysis:

the characteristics of a Thing (attribute):

color.

number of tires.

of Things ( function ) : run.

   Discovery: Things are actually made up of features (attributes) and behaviors (functions).

It is simple to understand that a property is a numerical value, which is actually a variable, and a function is a method.

Car {

color;

number of tires;

Run () { }

}

to define the format of a class

public class class name {

you can write 0 to n Properties

Data Type variable name 1;

Data Type variable name 2;

you can write 0 to n methods

modifier return value type Method Name ( parameter ) {

execute the statement ;

}

by computer language Java to describe this thing.

 Public class Car {    String color;     int Number ;     void run () {        + ":" + number );}    }

By describing the code, you know that the real meaning of the class is to describe things. Attributes and functions are collectively referred to as members of a thing.

the members of a thing are divided into two types: member properties and member functions. member properties are represented in code as member variables, and member functions are reflected in code as member methods.

Test the written code. Requires a class that can run independently.

Test class

 Public classCardemo { Public Static voidMain (string[] args) {/** Test: The Run method in the car class. */        //1, create an object for car. Give the object a name. Car C =NewCar ();//c is a variable of class type.        C points to an object of a specific car type. //2, the function of the object is called through an existing object.        Format: Object. Object member; //3, you can assign a value to the property of the object. C.color = "Red"; C.number= 4;    C.run (); }}

2.2memory plots for objects

The object-oriented programming idea tries to keep the description of things in the program consistent with the shape of the things in reality. In order to do this, the object-oriented idea presents two concepts, namely, classes and objects. the class is an abstract description of a certain kind of thing, and the object is used to represent the individual of that kind of thing in reality . The next step is to use a legend to abstract the relationship of the class to the object, as shown in.

in, the toy model can be seen as a class, a toy as an object, from the toy model and the relationship between the toy can be seen between the class and the relationship between the object. Class is used to describe common characteristics of multiple objects, which are templates for objects. An object is used to describe an individual in reality, which is an instance of a class .

2.3differences between local variables and member variables

difference One: the location of the definition is different , A variable defined in a class is a member variable. defined in the method or the variable inside the {} statement is a local variable.

Difference Two: the location in memory is different , member variables are stored in the Heap objects in memory , local variables are stored in the stack memory method.

Difference Three: The declaration period is different . The member variable appears in the heap as the object appears, disappearing from the heap as the object disappears . Local variables appear in the stack as the method runs, disappearing as the stack of methods pops up.

Difference Four: initialization differs , member variable because in heap memory, all default initialization values , Local variables do not have default initialization values and must be assigned manually before they can be used.

2.4Encapsulation Overview

Encapsulation, it is also one of the characteristics of object-oriented thinking. An object-oriented total of three features: encapsulation, inheritance, polymorphism. Next we learn about encapsulation in detail.

Package Performance:

1, the method is a most basic package body.

2, the class is actually a package body.

From the above two points, the benefits of encapsulation are as follows:

1, improve the reusability of the code.

2, hidden the implementation of details, but also to provide external access to the way. Facilitates the use of callers. This is one of the core, and can be understood as the concept of encapsulation.

3, improve the security.

Java basic syntax-object-oriented

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.