JAVA programmers must read: Basics (4) class and inheritance

Source: Internet
Author: User
Tags inheritance
This tutorial will begin to discuss the object lifecycle. This includes how to create an object, how to use it, and how to clear it from the system without using it. Here is an introduction:
4.1 lifecycle of an object
In this section, you can learn how to create and use any types of objects, and also discuss how to clear objects when the objects are no longer needed.
A typical JAVA program creates an object. The interaction between objects is achieved by sending messages. Through the interaction of these objects, JAVA programs can execute a GUI, run an animation, or send and receive information through the network. Once an object has completed the task, it is recycled as useless information, and its resources can be recycled by other objects.
The following is a small example of CreateObjectDemo, which creates three objects: a Point object and two Rectange objects. You need these three source programs to compile this program:
Public class CreateObjectDemo {
Public static void main (String [] args ){
// Create a Point object and two Rectangle objects
Point origin_one = new Point (23, 94 );
Rectangle rect_one = new Rectangle (origin_one, 100,200 );
Rectangle rect_two = new Rectangle (50,100 );
// Display the width, height, and area of rect_one
System. out. println ("Width of rect_one:" + rect_one.width );
System. out. println ("Height of rect_one:" + rect_one.height );
System. out. println ("Area of rect_one:" + rect_one.area ());
// Set the rect_two location
Rect_two.origin = origin_one;
// Display the rect_two position
System. out. println ("X Position of rect_two:" + rect_two.origin.x );
System. out. println ("Y Position of rect_two:" + rect_two.origin.y );
// Move rect_two and display its new location
Rect_two.move (40, 72 );
System. out. println ("X Position of rect_two:" + rect_two.origin.x );
System. out. println ("Y Position of rect_two:" + rect_two.origin.y );
}
}
Once an object is created, the program can operate on the object and display some information about it. The following is the output result of the program:
Width of rect_one: 100
Size of rect_one: 200
Area of rect_one: 20000
X Position of rect_two: 23
Y Position of rect_two: 94
X Position of rect_two: 40
Y Position of rect_two: 72
This section uses this example to describe the object lifecycle in a program. From this, you can learn how to write code to create and use objects, and how the system clears them from the memory.

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.