The concept of a class and object
object: Objects, all things are objects, objective things are objects. an object is an instance of a class that has state and behavior . For example, a dog is a
a object, its state is: color, name, variety, behavior: WAG tail, bark, eat and so on.
Class: A class is a template that describes the behavior and state of a class of objects.
Two objects in Java
Now let's take a closer look at what an object is. Look around the real world, you will find a lot of objects around, cars, dogs, people and so on. All of these
The elephant has its own state and behavior.
Take a dog for example, its state is: name, variety, color, behavior has: call, WAG tail and run.
Compare real objects with software objects, which are very similar.
Software objects also have states and behaviors. The state of the software object is the attribute, and the behavior is manifested by the method.
In software development, the internal state of the method operation object is changed, and the mutual invocation of the object is done by means of the method.
Three classes in Java
Class can be seen as a template for creating Java objects.
Use the following simple class to understand the definition of a class in Java:
public class dog{ String breed; int age; String color; void Barking () { // } void Hungry () { // } void Sleeping () { // }}
A class can contain the following types of variables:
a local variable: a variable defined in a method, construction method, or block of statements is called a local variable. Variable declaration and initialization are all in the method,
The variable is automatically destroyed when the method is finished.
b member Variable: A member variable is a variable that is defined in a class, outside the method body. This variable is instantiated when the object is created. Member variables can be
Class, methods, construction methods, and statement block access to specific classes.
Class C (Static) 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, in the example above: Barking (), hungry (), and sleeping () are all methods of the dog class.
Four Construction methods
Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for the class.
At least one constructor method is called when an object is created. The name of the constructed method must be the same as the class, and a class can have multiple constructors
Method.
Here is an example of a construction method:
public class puppy{public Puppy () { } public Puppy (String name) { //This constructor has only one parameter: name }}
Five Create objects
Objects are created from a class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:
A declaration: declares an object, including the object name and the object type.
B instantiation: Use the keyword new to create an object.
C Initialization: When you create an object with new, the constructor method is called to initialize the object.
Syntax format: Class name Object name =new class name ();
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 ("Passed name is:" + name); } public static void Main (String []args) { ///The following statement will create a Puppy object Puppy mypuppy = new Puppy ("Tommy");} }
Compiling and running the above program will print out the following results:
Six access instance variables and methods
Access member variables and member methods through the objects you have created, as follows:
Instantiate the object objectreference = new Constructor ();//access the variable objectreference.variablename;//the method in the class Objectreference.methodname ();
Instance
The following example shows how to access an instance variable and invoke a member method:
public class puppy{ int puppyage; Public Puppy (String name) { //This constructor has only one parameter: name System.out.println ("Passed name is:" + name); } public void Setage (int.) { puppyage = age; } public int getage () { System.out.println ("Puppy's is:" + puppyage); return puppyage; } public static void Main (String []args) { //Create object Puppy Puppy = new Puppy ("Tommy"); To set the Age puppy.setage (2) by means of the method; Call another method to get the Age puppy.getage (); You can also access the member variable System.out.println as follows ("Variable Value:" + puppy.puppyage);} }
Compile and run the above program to produce the following results:
Seven source file claim rules
When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.
A there can be only one public class in a source file.
b A source file can have multiple non-public classes.
The name of the C source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should
Life name is Employee.java.
D If a class is defined in a package, then the packages statement should be in the first row of the source file.
e 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 language
sentence should be in front of the source file.
The Fimport statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.
The G class has several access levels, and the classes are divided into different types: abstract and final classes.
In addition to some of the types mentioned above, Java also has some special classes, such as: Inner class, anonymous class.
Eight Java Packages
packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to divide classes and interfaces
Class.
Nine import statements
In Java, if you give a full qualified name, including the package name, the 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 will command the compiler to load all classes under the Java_installation/java/io path
Import java.io.*;
11 Simple examples
In this example, we create two classes: Employee and Employeetest.
First, open the text editor and paste the following code in. Note Save the file as Employee.java.
The employee class has four member variables: name, age, designation, and salary. The class explicitly declares a constructor method, which has only one
A parameter.
Import java.io.*;p ublic class employee{ String name; int age; String designation; Double salary; How to construct the Employee class public Employee (String name) { this.name = name; } Set the value of age public void setage (int newage) {Age = newage; } Set the value of designation public void Setdesignation (String newdesig) { designation = Newdesig; } Set the value of salary public void Setsalary (double newsalary) { salary = newsalary; } Print information public void Printemployee () { System.out.println ("Name:" + name); System.out.println ("Age:" + age); SYSTEM.OUT.PRINTLN ("designation:" + designation); System.out.println ("Salary:" + Salary);} }
The program starts with the main method. In order to run this program, you must include the main method and create an instance object.
The following is a employeetest class that instantiates an instance of 2 employee classes and invokes a method to set the value of a variable.
Save the following code in the Employeetest.java file.
Import java.io.*;p Ublic class Employeetest {public static void Main (String args[]) { //Create two objects using a constructor Employee Empone = new Employee ("James Smith"); Employee Emptwo = new Employee ("Mary Anne"); Call the members of both objects method Empone.setage (+); Empone.setdesignation ("Senior software Engineer"); Empone.setsalary (+); Empone.printemployee (); Emptwo.setage (+); Emptwo.setdesignation ("Software Engineer"); Emptwo.setsalary (+); Emptwo.printemployee (); }}
Compiling these two files and running the Employeetest class, you can see the following results:
Javase Getting Started learning 11:java object-oriented and objects