Java objects and classes
Java as an object-oriented language. The following basic concepts are supported:
- polymorphic
- inherit
- encapsulation
- abstract
- class
- object
- example
- method
- heavy load
In this section, we focus on the concepts of objects and classes. Recommended distributed architecture Source address source
- 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, and its status includes: color, name, variety; Behaviors include wagging tail, barking, eating, etc.
- Class: a class is a template that describes the behavior and state of a class of objects.
In the following figure, boy s and girl s are classes, and each person is an object of this class:
In the following figure, the car is a class, and each specific car is an object of the car class. The object includes the color, brand, name, etc. of the car.
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.
Create a simple class as shown in the figure above to understand the class definition in Java:
public class Dog { String breed; int size; String colour; int age; void eat() { } void run() { } void sleep(){ } void name(){ } }
A class can contain the following types of variables:
- Local variables: variables defined in methods, construction methods, or statement blocks are called local variables. Variable declaration and initialization are in the method. After the method ends, the variable will be destroyed automatically.
- Member variables: member variables are variables defined in the class and outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods in a class, constructor methods, and statement blocks of a specific class.
- Class variable: class variable is also declared in the class, outside the method body, but must be declared as static type.
A class can have multiple methods. In the above example, eat(), run(), sleep() and name() are all methods of the 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:
public class Puppy{ public Puppy(){ } public Puppy(String name){ // This constructor has only one parameter: name } }
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:
- Declaration: declare 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.
The following is an example of creating an object:
public class Puppy{ public Puppy(String name){ //This constructor has only one parameter: name System.out.println("What's the dog's name : " + name ); } public static void main(String[] args){ // The following statement will create a supply object Puppy myPuppy = new Puppy( "tommy" ); } }
Compiling and running the above program will print the following results:
What's the dog's name : tommy
Accessing instance variables and methods
Access member variables and member methods through the created object, as follows:
/* Instantiate object */ Object referenceVariable = new Constructor(); /* Accessing variables in a class */ referenceVariable.variableName; /* Accessing methods in classes */ referenceVariable.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("What's the dog's name : " + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("The dog's age is : " + puppyAge ); return puppyAge; } public static void main(String[] args){ /* create object */ Puppy myPuppy = new Puppy( "tommy" ); /* Set age by method */ myPuppy.setAge( 2 ); /* Call another method to get the age */ myPuppy.getAge( ); /*You can also access member variables like this */ System.out.println("Variable value : " + myPuppy.puppyAge ); } }
Compile and run the above program to produce the following results:
What's the dog's name : tommy The dog's age is : 2 Variable value : 2
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.
- There can only be 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, 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, the import statement should be at the top of the source file.
- The import statement and package statement 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 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
import java.io.*;
A simple example
In this example, we create two classes: Employee and EmployeeTest.
First open the text editor and paste the following code. Note that 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.
Employee.java file code:
import java.io.*; public class Employee{ String name; int age; String designation; double salary; // Constructor of 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 design*/ 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. 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 the EmployeeTest.java file.
EmployeeTest.java file code:
import java.io.*; public class EmployeeTest{ public static void main(String[] args){ /* Use the constructor to create two objects */ 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("Rookie programmer"); empTwo.empSalary(500); empTwo.printEmployee(); } }
Compile these two files and run the EmployeeTest class. You can see the following results:
$ javac EmployeeTest.java $ java EmployeeTest name:xxxB1 Age:26 position:Senior programmer salary:1000.0 name xxx2 Age:21 position:Rookie programmer salary:500.0