Java objects and classes

Java as an object-oriented language. The following basic concepts are supported:

In this section, we focus on the concepts of objects and classes.

Objects in Java

Now let's take a closer look at what objects are. Look at the real world around you and you will find many objects, cars, dogs, people and so on. All of these objects have their own state and behavior.

Take a dog for example. Its state includes: name, variety, color, and its behavior includes barking, wagging its tail and running.

Compared with real objects and software objects, they are very similar.

Software objects also have states and behaviors. The state of a software object is an attribute, and its behavior is reflected by methods.

In software development, the method operates the change of the internal state of the object, and the mutual call of the object is also completed through the method.

Classes in Java

Class can be seen as a template for creating Java objects.

Understand the definition of classes in Java through the following simple class:

A class can contain the following types of variables:

A class can have multiple methods. In the above example, barking (), hungry () and sleeping () are all methods of dog class.

Construction method

Each class has a constructor. If you do not explicitly define a constructor for a class, the java compiler will provide a default constructor for the class.

When creating an object, at least one constructor must be called. The name of the constructor must have the same name as the class. A class can have multiple constructors.

The following is an example of a construction method:

create object

Objects are created from classes. In Java, the keyword new is used to create a new object. Creating an object requires the following three steps:

The following is an example of creating an object:

Compiling and running the above program will print the following results:

Accessing instance variables and methods

Access member variables and member methods through the created object, as follows:

example

The following example shows how to access instance variables and call member methods:

Compile and run the above program to produce the following results:

Source file declaration rules

In the last part of this section, we will learn the declaration rules of the source file. Pay special attention to these rules when defining multiple classes in a source file, as well as import statements and package statements.

Classes have several access levels, and classes are also divided into different types: abstract classes and final classes. These are described in the access control section.

In addition to the types mentioned above, Java also has some special classes, such as internal classes and anonymous classes.

Java package

Package is mainly used to classify classes and interfaces. When developing Java programs, hundreds of classes may be written, so it is necessary to classify classes and interfaces.

Import statement

In Java, if you give a complete qualified name, including package name and class name, the java compiler can easily locate the source code or class. The import statement is used to provide a reasonable path so that the compiler can find a class.

For example, the following command line commands the compiler to load Java_ All classes under the installation / Java / Io path

A simple example

In this example, we create two classes: employee and employeetest.

First open the text editor and paste the following code. Note to save the file as employee java。

The Employee class has four member variables: name, age, design, and salary. This class explicitly declares a constructor that has only one parameter.

Programs are executed from the main method. To run this program, you must include the main method and create an instance object.

The employeetest class is given below. This class instantiates two instances of Employee class and calls methods to set the value of variables.

Save the following code in employeetest Java file.

Compile these two files and run the employeetest class. You can see the following results:

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
分享
二维码
< <上一篇
下一篇>>