Catalog
4. Class loading (currently known)
Loading includes (passive, active)
1. Classes and Objects
Everything is object
Class definition class: is the template and blueprint for constructing objects, class is a collection of related attributes and methods, class is an abstract concept, it is artificially defined and not meaningful things according to the data type, class is a reference data type
Definition of object
Object: Object is the concrete implementation of class, is the real thing in the real world, has all the properties and methods of class
Understand:
In general life, there are many objects before they can be artificially defined as a category, which can be included to better describe these objects. In the java object-oriented language, a class is defined first, then many objects are created. Class is a collection of common features that many objects have. Objects are their specific manifestations. For example, automobile is a class, automobile has some attributes and methods (behavior). The object of automobile is a sports car, jeep, cross-country vehicle, and so on. These objects also have the attributes and methods of automobile.
2. Composition of classes
Form |
---|
1. Attributes: are the characteristics of things (such as the color, material of mobile phones, etc.) |
2. Behavior (method): is what things can do (e.g., mobile phones can make phone calls, send text messages, etc.) |
Classes can have other parts, such as code blocks
static{ content } Static code block, loaded with class loading, does not exist with object, because the class only needs to be loaded once in the method area, so this static code block only needs to be called once { content } Non-static code blocks, loaded with object creation, need to be called every time they are created
Member variables of classes
Member variables: It can be a reference type or a basic data type [Access modifier][Modifier]Variable Name; Access modifiers: public protected default void Modifier static Class member variables can be assigned directly at definition time [][]Variable Name=numerical value; If there is no active definition or initialization using a parameterized constructor, the default constructor automatically assigns values to member variables:: int 0,short 0, byte 0, long 0, float 0.0,double 0.0,char Spaces, boolean false,String null
How to access member variables:
1. Non-static member variable, accessed by object name, i.e. object name. member variable;
2. It is a static member variable, which can be accessed either by the variable name or directly by the class name.
Member methods of classes
When a class method is used, the return value is returned to the place where it was called. Then the method pops up the stack area. When the main method is executed, the whole program exits.
Definition of member method: [Access modifier][Modifier]Return value type method name(Hump)(parameter list...){ Method Body; return Return value;//If it is a constructor, omit the return statement If it is a void type, omit the return return value or write the return directly (direct end method) } Be careful: A method can have at most one return value if it has one The return value can be either a basic data type or a reference data type return The return value return value type must be consistent or compatible with the defined type(Can be automatically converted) Method can no longer be defined inside a method, method nesting is not allowed Call to method
3. Create a class
Create a class by using the class keyword to create the basic form of the class:
General Class:[Access modifier]class Class name stay.java Only one can be allowed in a file public Modified Classes Internal Class:[Access modifier][Modifier]class Class name
4. Class loading (currently known)
There are stacks, stacks, and method areas in the JVM virtual machine
Heap: Stores reference type data, local variables
Stack: Stores member variables, objects
Method Zone: There is a zone dedicated to class loading, a zone for static
Loading includes (passive, active)
Passive loading:
Active loading:
· new Keyword: Create reference type variables in the stack, store real objects in the stack · Access the static member of the class, which is not final Modification(static final Modified members are stored directly in the class without calling,And must actively assign initial values) · When inheriting, the active class loads all the information of the parent class,Loading parent class into method area · Java A class that is marked as a startup class when the virtual machine starts, that is, contains Main Classes of methods
Loading order in a class
1. Load the static-modified part belonging to the class first (when transporting the part modified by the static final, you can call it directly from the class, but you don't need to load the class). The static-modified part is stored in the method area and is automatically initialized
2. Then load the non-static part that belongs to the object
Be careful:
Static members are initialized only once when the class is loaded, and only one
Non-static members are initialized as objects are created, and several objects are initialized several times
Static methods can only use static members
Non-static methods can use either static or non-static members
5. Create Objects
Create an object of a class with the new key
For example, there is one Student Class, create a xiaoMing Objects of this class Student xiaoMing = new Student();(0x110) To the right of the assignment number is a real object, stored in the heap To the left of the assignment number is only one Student The variable whose name is xiaoMing,Store in Stack Essential: is the address of the object that will be the heap area(0x110)Assigned to xiaoMing This variable is defined by the xiaoMing This variable points to this real object in the heap area
6. Constructors
Definition of constructor: constructor name and class name are the same, constructor does not return value, constructor is called automatically when creating object, if there is no explicit definition constructor then the system will automatically have a default parametric constructor, if its own constructor is defined, then the default constructor will be overwritten unless it is exposed again
Initialization involving objects
For example, the above example:
For example, there is one Student Class, create a xiaoMing Objects of this class Student xiaoMing = new Student();(0x110) This one on the right is the real object and Student()Is a parameterless constructor that initializes members of an object
Constructors can be parameterized or not, so there can be many constructors in a class with different parameters (type of parameters, order of parameters, length of parameters) to initialize member variables of objects, which is an overload of methods, and constructors are a special method
Method overload:
There can be many methods in a class with the same method name but different parameter lists:
Different parameter lists:
1. The types of parameters are different
2. The type data of the parameters are different
3. The length of the parameters is different
Purpose: In a class where there may be parameters with the same method but different roles, the overload of the method will solve the problem perfectly
For example: public int max(int a,int b){ return a>b?a:b; } public double max(double,double b){ return a>b?a:b; } Both return the maximum of two numbers, but one is the comparison int Type, one is comparative double Type, the overload of the method used, the corresponding parameter list input, the program runs, the corresponding method will be automatically selected