Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic concepts of objects and classes in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the basic concepts of objects and classes of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the basic concepts of objects and classes of Java".

Object: an object is an instance of a class (the object is not looking for a girlfriend), with state and behavior. For example, a dog is an object, its status is: color, name, breed; behavior: wagging tail, barking, eating and so on.

Class: a class is a template that describes the behavior and state of a class of objects.

In the following picture, boys and girls are the class, and each person is the object of the class:

Objects in Java

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

Take a dog as an example, its status is: name, breed, color, behavior: barking, wagging tail and running.

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

Software objects also have states and behaviors. The state of the software object is the property, and the behavior is reflected by the method.

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

Classes in Java

Class can be thought of as a template for creating Java objects.

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

Public class Dog {

String breed

Int age

String color

Void barking () {

}

Void hungry () {

}

Void sleeping () {

}

}

A class can contain the following types of variables:

Local variables: variables defined in methods, constructors, or statement blocks are called local variables. Variable declaration and initialization are in the method, and after the method ends, the variable is automatically destroyed.

Member variables: member variables are defined in the class, outside the body of the method. This variable is instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.

Class variables: class variables are also declared in the class, outside the method body, but must be declared as a static type.

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

Construction method

Every 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.

At least one constructor must be called when creating an object. The name of the constructor must be the same as the class, and a class can have more than one constructor.

The following is an example of a constructor:

Public class Puppy {

Public Puppy () {

}

Public Puppy (String name) {

/ / this constructor has only one parameter: name

}

}

Create object

The object is created from the class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

Declare: declare an object, including the object name and object type.

Instantiation: use the keyword new to create an object.

Initialization: when you create an object using new, the constructor is called to initialize the object.

Here is an example of creating an object:

Public class Puppy {

Public Puppy (String name) {

/ / this constructor has only one parameter: name

System.out.println ("puppy's name is:" + name)

}

Public static void main (String [] args) {

/ / the following statement creates a Puppy object

Puppy myPuppy = new Puppy ("tommy")

}

}

Compiling and running the above program will print out the following result:

The puppy's name is: tommy accesses instance variables and methods

Access member variables and member methods through the objects you have created, as follows:

/ * instantiate object * /

ObjectReference = new Constructor ()

/ * access variables in the class * /

ObjectReference.variableName

/ * access methods in the class * /

ObjectReference.methodName ()

Example

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

Public class Puppy {

Int puppyAge

Public Puppy (String name) {

/ / this constructor has only one parameter: name

System.out.println ("puppy's name is:" + name)

}

Public void setAge (int age) {

PuppyAge = age

}

Public int getAge () {

System.out.println ("the age of the dog is:" + puppyAge)

Return puppyAge

}

Public static void main (String [] args) {

/ * create an object * /

Puppy myPuppy = new Puppy ("tommy")

/ * set age by method * /

MyPuppy.setAge (2)

/ * call another method to get age * /

MyPuppy.getAge ()

/ * you can also access member variables as follows * /

System.out.println ("variable value:" + myPuppy.puppyAge)

}

}

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

The puppy's name is: tommy the puppy's age is: 2 variable value: 2 source file declaration rule

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

There can be only one public class in a source file

A source file can have multiple non-public classes

The name of the source file should be consistent with the class name of the public class. For example, if the class name of the public class in the source file is Employee, then the source file should be named Employee.java.

If a class is defined in a package, the package statement should be on the first line of the source file.

If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, then the import statement should come first in the source file.

Import statements and package statements are valid for all classes defined in the source file. You cannot declare different packages for different classes in the same source file.

Classes have several levels of access, and classes are divided into different types: abstract classes, final classes, and so on. These will be described in the access control section.

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

Java package

Packages are 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, then 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 will command the compiler to load all classes under the java_installation/java/io path

A simple example of import java.io.*;

In this example, we create two classes: Employee and EmployeeTest.

First open the text editor and paste the following code in. Be careful to save the file as Employee.java.

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

Employee.java file code:

Import java.io.*

Public class Employee {

String name

Int age

String designation

Double salary

/ / the constructor of the Employee class

Public Employee (String name) {

This.name = name

}

/ / set the value of age

Public void empAge (int empAge) {

Age = empAge

}

/ * set the value of designation * /

Public void empDesignation (String empDesig) {

Designation = empDesig

}

/ * set the value of salary * /

Public void empSalary (double empSalary) {

Salary = empSalary

}

/ * print information * /

Public void printEmployee () {

System.out.println ("name:" + name)

System.out.println ("Age:" + age)

System.out.println ("position:" + designation)

System.out.println ("salary:" + salary)

}

}

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

Here is the EmployeeTest class, which instantiates two instances of the Employee class and calls the method to set the value of the variable.

Save the following code in the EmployeeTest.java file.

EmployeeTest.java file code:

Import java.io.*

Public class EmployeeTest {

Public static void main (String [] args) {

/ * create two objects using the constructor * /

Employee empOne = new Employee ("RUNOOB1")

Employee empTwo = new Employee ("RUNOOB2")

/ / call the member methods of these two objects

EmpOne.empAge (26)

EmpOne.empDesignation ("Senior programmer")

EmpOne.empSalary (1000)

EmpOne.printEmployee ()

EmpTwo.empAge (21)

EmpTwo.empDesignation ("novice programmer")

EmpTwo.empSalary (500)

EmpTwo.printEmployee ()

}

}

By compiling the two files and running the EmployeeTest class, you can see the following results:

$javac EmployeeTest.java$ java EmployeeTest name: RUNOOB1 Age: 26 position: senior programmer salary: 1000.0 name: RUNOOB2 Age: 21 position: rookie programmer salary: 500.0 Thank you for your reading, the above is the content of "basic Concepts of objects and classes of Java". After the study of this article, I believe you have a deeper understanding of the basic concepts of objects and classes of Java. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report