Java class definition and execution sequence learning tutorial

Class must be defined before it can be used. Class is a template for creating objects. Creating objects is also called class instantiation.

The following is a simple example to understand the definition of classes in Java:

Description of the example:

A class can contain the following types of variables:

Construction method

The method automatically executed during class instantiation is called constructor, which does not need to be called manually. Constructor can do some initialization during class instantiation.

The constructor name must be the same as the class name and have no return value.

Each class has a constructor. If not explicitly @ r_ 419_ 2488 @ constructor, the java compiler will provide a default constructor for this class.

The following is an example of a construction method:

Operation results:

explain:

create object

An object is an instance of a class. The process of creating an object is also called class instantiation. Objects are created using classes as templates.

In Java, using the new keyword to create an object generally has the following three steps: Declaration: declaring an object, including object name and object type. Instantiation: use the keyword new to create an object. Initialization: when using new to create an object, the constructor will be called to initialize the object.

For example:

You can also initialize while declaring:

Accessing member variables and methods

Access member variables and member methods through created objects, such as:

The following example demonstrates how to access member variables and methods:

Operation results:

Basic running order of Java classes. Let's illustrate the running order of a basic Java class with the following classes:

The basic operation sequence is:

As a programmer, we should know the basic operation process of the program, otherwise we will be confused, which is not conducive to the writing of code and the development of technology.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>