Class and object of Java object-oriented programming

Source: Internet
Author: User

  Important thing to say three times

We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings , animals and so on these classes constitute the world.

We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings, animals and so on these classes constitute the world.

We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings, animals and so on these classes constitute the world.

    One, create a class in Java

Syntax class Name {

Properties

Method

}

In this sentence, the class name is our abstraction of a series of things that have a common attribute, such as our class name "person" is to abstract people into a class, people have height, weight, name and so on, these are the attributes of the person class, define the name of this attribute we can write as

String name;

People have activities, such as talking, talking this behavior is the method in the above statement, the definition of a conversation this method can be written as

void Talk () {method Body}

The method body is the specific execution process that describes the behavior of the conversation.

  Ii. Creation of objects

 The person class is created so that we can use it to create an object that is specific to someone, such as

Person Itliu = new person ();

So we have itliu this person created, in this statement Itliu is the object name of the object we created, also known as a reference, it is like a person's name. The equal sign of "=" is to assign the newly created object to the Itliu reference. The reference is stored in the stack memory, the object is in the heap memory, and the reference in the stack memory points to the corresponding object in the heap memory.

An object has properties that we call a member variable

The behavior of an object we call it a member function

For example:

Let's say we've defined the person class.

Person Itliu = new person ();

Itliu.name; Age attribute of Itliu

Itliu.jump (); Tiliu's Jump behavior

Creation and use of anonymous objects

New person (). Jump;

This sentence we can understand, I saw a beautiful woman jumping on the street, but I didn't know her name.

Note: The anonymous object is usually one-time, which is like you see the beautiful woman jump, but you do not know her name so you and she just one side no longer see.

Class and object of Java object-oriented programming

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.