1. Classes and objects
-
What is the class
- Classes are formed by combining the static properties and dynamic operations of a class of things.
- Classes are abstract, simulating a class of things;
- A class is a design template for an object and does not occupy memory. Memory is allocated according to the design of the class when creating an object.
-
What is an object
- An object is an entity of a class;
- The object is concrete and actually exists;
- Objects have a life cycle;
For example, a class can be likened to a design drawing of a car, from which cars are produced objects:
2. Class definition and object creation
2.1. How is a class defined?
- Define class use
class
Key words; - Variables defined in a class are called properties of that class;
- The functions defined in a class are called the methods of that class;
For example, define a Car class:
class Car
{
//Car attributes
int number; // Vehicle id
//Car class method
void go(a)
{
// Methods in a class access attributes of the class
System.out.printf("My number is %d, go go go!", number); }}Copy the code
2.2. How do I create an object?
Create an object of class Car in the main method and access its members and methods:
class CarTest
{
public static void main(String[] args)
{
// Create a dynamic object
Car testCar = new Car();
// Access the properties of the object and modify them
testCar.number = 1;
// Call the method of the objecttestCar.go(); }}Copy the code
Compile and run:
javac CarTest
java CarTest
Copy the code
Running results:
3. Class and object memory allocation details (key)
!!!!!!!!! Memory here refers to the memory occupied by the program while it is running, which can be understood as RAM space. !!!!!!!!!
- Write good program source code, execute
javac CarTest
The resultingCarTest.class
The executable program is saved on the hard disk;
- When we execute
java CarTest
After the command,The operating system loads the entire program into memory, ready for operation; - The program starts with the main entry function;
- The program allocates memory during execution. Let’s start parsing step by step:
3.1. How is the memory space allocated for classes?
A class is just a concept, it’s abstract, it simulates an object, in this case a vehicle, so it doesn’t take up any memory.
3.2. How is the object’s memory allocated?
Car testCar = new Car();
Copy the code
Car()
This function is called the Car constructor. Although we haven’t defined it, it exists by default and is called automatically when an object of this class is created, as described in Section 4 (P.new
The keyword indicates that memory space is allocated dynamically when this object is created, soAn object that comes out new has memory space in the heap.new
The role of keywords and C languagemalloc
The function does the same thing, but is simpler.The system allocates memory in the heap as defined in the class, and returns the head address;- The first address of the object is returned and stored in the testCar variable, so in C testCar refers to the Car object in the heap, and in Java testCar refers to the Car object in the heap.
- TestCar is a variable of type Car and is defined in the main function, so it is a local variable whose memory is allocated on the stack.
3.3. How are properties and methods of objects in the heap accessed?
As shown in the figure above, if we want to access the number attribute and go method of the Car object in the heap, how do we do that?
The testCar variable holds the first address of the Car object, so we can do anything with it!
In the demo above, you can access the number attribute directly from testcar.nunber and the go method from testcar.go ().
4. Constructor
4.1. The role of constructors
When an object is created, the system first allocates memory for the object and then automatically calls the constructor of the class.
4.2. Default no-parameter constructor
When creating the object, we call the Car() function, but this function is not in our class definition, so how does this function get called?
Car testCar = new Car();
Copy the code
Car() is the Car constructor. After we define the Car class, the default constructor Car() is automatically generated.
4.3. Custom constructors
When customizing constructors, note that constructors have the following characteristics:
- The function name is the same as the class name
- Parameters can be none or multiple
- There is no return value
- Constructors must be public or default modifiers
- You can have multiple constructors
- Once you customize the constructor, the system no longer automatically generates the default no-parameter constructor
For example, here I define a parameterless constructor and a parameterless constructor:
class Car
{
//Car attributes
int number; // Vehicle id
// No argument constructor
public Car(a)
{
System.out.printf("The no-argument constructor is called.\n");
}
// Has a parameter constructor
public Car(int i)
{
number = i;
System.out.printf("Parameter constructor called.\n");
}
//Car class method
void go(a)
{
// Methods in a class access attributes of the class
System.out.printf("My number is %d, go go go! \n", number); }}Copy the code
Create an object using the parameterless and parameterless constructors:
class CarTest
{
public static void main(String[] args)
{
// Create a dynamic object using the no-argument constructor
Car testCar1 = new Car();
// Access the properties of the object and modify them
testCar1.number = 1;
// Call the method of the object
testCar1.go();
// Use the argument constructor to create a dynamic object
Car testCar2 = new Car(2);
// Call the method of the objecttestCar2.go(); }}Copy the code
The running results are as follows:
To verify “when we customize the constructor, does the system automatically generate the default constructor? “We,”Remove the no-argument constructorThe default no-argument constructor is not defined:
So we can get:
Once a custom constructor is defined, the system no longer automatically generates the default constructor.
4.4. Assignment of data members in constructors
For the data members of the class (attributes of the class), the system executes the initial values assigned when they are defined, and then the initial values assigned in the constructor.
For example, if I assign the number attribute to 1 and the constructor to 2, the result is 2:
class Car
{
//Car attributes
int number = 1; // Vehicle id
// No argument constructor
public Car(a)
{
System.out.printf("The no-argument constructor is called.\n");
}
// Has a parameter constructor
public Car(int i)
{
number = i;
System.out.printf("Parameter constructor called.\n");
}
//Car class method
void go(a)
{
// Methods in a class access attributes of the class
System.out.printf("My number is %d, go go go! \n", number); }}class CarTest
{
public static void main(String[] args)
{
// Use the argument constructor to create a dynamic object
Car testCar = new Car(2);
// Call the method of the objecttestCar.go(); }}Copy the code
Running results:
To receive more exciting articles and resources, please subscribe to my wechat official account: “McUlover666”.