1, Overview of classes and objects
Classes and objects are the core of oriented programming. Class is used to describe the common characteristics of a class of objects in the objective world; An object is a concrete instance of a class. Before you know a complete Java program, make sure you understand a few nouns.
What is an object?
- Official explanation: an object is an entity that encapsulates data and its related methods. Data is the attribute of an object (also known as member variable). The method is the operation on the data to realize specific functions. The attribute value of an object determines the state of the object, and the operation method of the object determines the behavior ability of the object.
- Comparing an individual to an object, the individual's weight, height, three outlooks, achievement and so on are the attributes of the object (i.e. the individual's member variables). If an individual wants to become better, he decides to spend a lot of time learning (Methods) to improve his performance (attributes), that is, the operation of the object's use method on the data, Achieved better performance (function).
What is class?
- Official explanation: any object in reality belongs to a class. A class is used to describe the common characteristics of multiple objects. It is an abstract data type. Creating a class in Java is to create a new data type. The variables defined by this type are collectively called reference variables, that is, the class is a reference type.
- In a program, a class is defined only once, but one or more objects of the class can be created. For example, the company in the Internet industry is a class, which creates some companies such as BAT, and these individuals (objects) can be abstracted into companies (classes) that conceptualize the Internet.
What is a constructor?
- Explanation: constructor is a special method. Each class has a constructor, which is also called construction method. The constructor is used to construct the instance of this class. Java language calls the constructor through the new keyword to instantiate the object. The function of the constructor is to instantiate the object and convert the written class into an object.
- Constructor type: default construction method, according to the custom construction method
- Characteristics of construction method:
1. The method name of the construction method must be completely consistent with the class name;
2. The constructor cannot have a return value. The constructor is mainly used to initialize the object
3. The constructor is called when instantiating the (new) object
4. The default construction method has no parameters and the method body is empty
5. A class can have multiple construction methods
6. If there is no construction method defined in the class, the compiler will automatically provide the default construction method. If there is a custom construction method in the class, Java will not automatically add the default construction method during compilation
Relationship between class and object
As mentioned earlier, a class represents a common abstract reference type (in addition, the biggest difference between a reference type and a basic data type is that a reference type requires memory allocation and use, and String is also a common reference type), and an object represents an independent individual
- A class is a template of an object, and an object is an instance of a class
- Classes can only be used through objects, that is, classes must be "instantiated" to get objects
- Class cannot be used directly, object can be used directly
2, Definition and use of classes and objects
Learned a lot of conceptual knowledge, and then combined with the code.
How to create a class?
Simple syntax for defining a class
Insert code slice here [Modifier ] class Class name{ Zero or more variables (data fields) Zero or more constructors Zero or more methods }
give an example:
/** * Define human */ class People { //Data field int age; String name; People() { //Default construction method } //Custom construction method People(int a) { age = a; } //void declares that the function has no parameters and no return value void selfInfo(String string) { System.out.println("My age is" + age); System.out.println("My name is" + name); System.out.println(string); } }
explain
- The modifiers in front of the class can be public, final, abstract, or default (not added)
- Class is the keyword that defines the class
- The class name shall conform to the naming rules of the identifier (big hump: the first letter of the word is uppercase and the rest is lowercase; it is composed of numbers, letters, and $, and the number cannot be at the beginning)
- The class body part can define multiple member variables. The constructor is used to construct the instance of the class. The Java language calls the constructor to instantiate one or more objects of the class through the new keyword. The format is as follows
Class name object name = new Class name([Argument list]);
- The above code cannot be used because only classes are defined and objects cannot be created and used
How to create and use objects?
Defines the syntax of the object
You need to use the above concept: instantiating objects.
A class is a reference data type and cannot be used directly after the class is defined. To use it, you need to convert the class into an object. How to convert it, you need to declare the object first and then instantiate the object:
Class name object name; Object name = new Class name([Argument list]); People p; //statement p= new People(); //instantiation Equivalent to Class name object name = new Class name([Argument list]); People p = new People();
The format of reference object member variable is:
Object name.attribute; p.name = "Li Hong";
The format of reference object method is:
Object name.Method name([Argument list]); p.seifInfo();
give an example
public class ExamplePrac2 { public static void main(String[] args) { People p1 = new People(); //Instantiate object 1 People p2 = new People(); //Instantiate object 2 p1.age = 1; //Reference object member variable p2.age = 2; p1.name = "Li Hong"; p2.name = "Wang Ming"; p1.selfInfo();//Reference object method p2.selfInfo(""); } } /** * Define human */ class People { int age; String name; People() { //Default construction method } //Custom construction method (i.e. method overloading) People(int a) { age = a; } People(String n) { name = n; } //void declares that the function has no parameters and no return value void selfInfo(String string) { System.out.println("My age is" + age); System.out.println("My name is" + name); } }
explain
- A source program Java can be composed of multiple class classes
- Explain why reference types need to instantiate objects with the new keyword:
As mentioned earlier, classes are reference types, which require memory allocation and use. The main functions of new are to allocate memory space, return object references and call constructors - To analyze why to instantiate objects from the perspective of memory, first, clarify the concept of memory space:
1. Heap memory: save the attribute content of the object
2. Stack memory: save the address of heap memory. Only the data written in stack memory can be saved successfully.
3. The relationship between stack memory and heap memory: one object can point to a heap memory, multiple objects can point to a heap memory, and a stack stack cannot have multiple heap memory allocated to it
For example:
When the new object is not, the memory allocation method is:
It can be seen that a null value is written in the heap memory. Generally, when the new object is not, the system will throw a null pointer exception after running
Exception in thread "main" java.lang.NullPointerException at com.company.ExamplePrac2.main(ExamplePrac2.java:14)
After using the new keyword, the system will allocate internal training space to the stack
How to create a constructor
Defines the syntax of the constructor
Class name{ Class name (custom parameter){ } }
give an example
class People { int age; String name; People() { //Default construction method } //Custom construction method (i.e. method overloading) People(int a) { age = a; } People(String n) { name = n; } }
explain
- What is a constructor
- Construction method can be overloaded: overloaded means that the method name is the same, the type of user-defined parameters can be different, the number of parameters can be different, and the order of parameters is different under the same number of parameters
- It has nothing to do with the return value and modifier
Complete code
package com.company; /** * @Author: qp * @Time: 2021/6/17 14:31 * @Description Creating and working with objects exercises */ public class ExamplePrac2 { public static void main(String[] args) { People p1 = new People(); People p2 = new People(); p1.age = 1; p2.age = 2; p1.name = "Li Hong"; p2.name = "Wang Ming"; p1.selfInfo("Java Language is easy to learn"); p2.selfInfo(""); People p3 = new People(1, "Li Hong"); //Call the method with two parameters to create the object p3 p3.selfInfo("Hello Word"); } } /** * Define human */ class People { int age; String name; People() { //Default construction method } //Custom construction method (i.e. method overloading) People(int a) { age = a; } People(String n) { name = n; } People(String n, int a) { name = n; age = a; } People(int a, String n) { name = n; age = a; } //void declares that the function has no parameters and no return value void selfInfo(String string) { System.out.println("My age is" + age); System.out.println("My name is" + name); System.out.println(string); } }
Output result:
My age is 1 My name is Li Hong Java Language is easy to learn My age is 2 My name is Wang Ming My age is 1 My name is Li Hong Hello Word Process finished with exit code 0
3, Summary
In order to improve the efficiency of learning new things, you can first disassemble and analyze the concepts you don't understand. The idea of this paper is to start with the three concepts of object, class and constructor, then analyze the concepts in detail with code, and finally summarize your ideas and explain the key points. For complex concepts, you can also try to use this idea to learn.