Object-oriented overview,

Source: Internet
Author: User

Object-oriented overview,

This topic explains what object orientation is, What are local and member variables, object-oriented features, how to create and use objects.

Object-oriented

Object-oriented is relative to process-oriented:

Both object-oriented and process-oriented thinking:

Process-oriented:

It emphasizes functional behavior:

Object-oriented:

Encapsulate functions into gold objects, emphasizing functional objects;

Object-oriented is based on process-oriented;

Concepts of objects and classes:

1. The object uses computer language to describe the things in the problem domain. The object uses "attributes" and "methods" to correspond to the static and dynamic attributes of the things respectively.

2. A class is an abstract concept used to describe objects of the same class shape. The class defines the static and dynamic attributes of such objects;

 

Object-oriented features:

1. encapsulation (encapsulation );

2. inheritance (inheritance );

3. polymorphism (polymorphism );

Class Definition

Describing things in life is nothing more than describing the properties and methods of things:

For example, a person has attributes such as height and weight, and has behaviors such as speaking and playing:

The same is true for describing things using classes in Java:

Attribute: member variables in the corresponding class:

Behavior: member functions (methods) in the corresponding class );

Differences between member variables and local variables:

1. member variables:

1): member variables are defined in the class and can be accessed throughout the class;

2) member variables are created with the object and exist in the heap memory where the object is located:

3) member variables have default initialization values;

2. Local variables:

1) local variables are defined only within the local range, for example, within the function and within the statement;

2) local variables exist in the stack memory;

3) The function range ends and the variable space is automatically released;

4) No Default initialization value for local variables;

Create an object. Use the following object:

Class Car {

String color = "red ";

Int num = 4;

Void show (){

System. out. println ("color =" + color + ". num" + num );

}

}

Class CarDemo {

Public static void main (String [] args ){

Car c = new Car (); // create an object

C. color = "black"; // modify the object field

C. show (); // use the object function

}

}

Anonymous object

Anonymous objects are simplified objects.

Two usage cases of anonymous objects:

1) when the object method is called only once;

2) Anonymous objects can be passed as actual parameters;

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.