First see JAVA-object-oriented-class and object-primary evolution 08
The relationship between class and object
Class is an abstract data type, is the overall description and definition of a certain thing
Objects are concrete realizations of abstract concepts
Create object
Use the new keyword
In addition to allocating memory space, the object of the creation number will be initialized by default and the constructor in the class will be called
A project should only have 1 main method
Object type
Reference type: Operate stack by reference—>heap
Basic types (8 basic types)
Attributes
Field:
- Default initialization number: 0, 0.0
- char: u0000
- boolean:false
- Reference: null
Constructor
Constructor is also called constructor
Features:
- Must be the same as the class name
- There must be no return type, and void cannot be written
- Once there is a parameter structure in the definition, no parameter must be explicitly defined
effect:
-
When new, essentially call the constructor
-
Used to initialize the value
Nature
Organize code in the way of classes and encapsulate data in the form of objects