Three: Object-oriented: ★★★★★
Features: 1 : Simplifying complex things.
2 : Object-oriented converts the performer in the previous process into a conductor.
3 : Object-oriented thinking is in line with the thinking habits of people today.
How do processes and objects manifest in our programs? The process is actually a function, and the object is to encapsulate some content such as functions.
Anonymous object usage Scenarios :
1: Anonymous objects can be used when the other method only makes one call.
2: You cannot use an anonymous object when an object makes multiple calls to a member. The object must be given a name.
The definition in a class is actually called a member. There are two types of members:
1: Member variables : In fact, the corresponding is the property of the thing.
2: member function : In fact, the corresponding is the behavior of things.
So, in fact, defining a class is defining member variables and member functions. But before the definition, it is necessary to analyze the property and behavior of things before they can be embodied in code.
private int age; // private access is minimal and only access in this class is valid.
Note: Private is only a form of embodiment of encapsulation.
Private members: Other classes can not directly create object access, so only through this class to provide specific access to the private access to complete the use of a function to provide access to it in the form of external.
Benefits: You can add logic judgment and other operations to the function, judge the data, and so on.
Summary: When developing, remember that attributes are used to store data, are accessed directly, are prone to security risks, so the properties in the class are usually privatized and provide public access methods externally.
This method generally has two, the canonical wording: for attribute xxx, you can use Setxxx (), GetXXX () to manipulate it.
Why is the main function not defined in the class?
Note: The main function exists only if the class needs to run independently, and if it is not needed, the main function is not defined.
interpretation of the main function: to ensure the independent operation of the class, is the entry of the program, called by the JVM.
The difference between a member variable and a local variable:
1 : Member variables are defined directly in the class.
a local variable is defined in a method, on a parameter, in a statement.
2 : Member variables are valid in this class.
A local variable is valid only within the curly braces it belongs to, the curly brace ends, and the local variable loses scope.
3 : Member variables exist in heap memory and disappear as objects are created.
A local variable exists in the stack memory and is released as the owning region runs and ends.
constructor : Used to initialize an object, it is to initialize the corresponding object, it is targeted, one of the functions.
features :
1: The name of the function is the same as the name of the class in which it resides.
2: You do not need to define a return value type.
3: The function does not have a specific return value.
Remember: When all objects are created, they need to be initialized before they can be used.
Note: when a class is defined, if a constructor is not defined, the constructor for an empty argument is automatically generated in the class, and initialization is done to facilitate the class to create the object. If a constructor is customized in a class, then the default constructor is gone.
In a class, you can have multiple constructors, because their function names are the same, so they can only be distinguished by a parameter list. Therefore, if more than one constructor is present in a class. Their existence is manifested by overloading.
What is the difference between a constructor and a general function?
1: Two function definition formats are different.
2: constructors are called when an object is created, used for initialization, and initialization actions are performed only once.
The general function is that after the object is created, it needs to be called to execute and can be called multiple times.
When do you use constructors?
When you analyze things, when you find specific things that appear, you have features that define them within the constructor.
What is the difference between constructing a code block and a constructor function?
Constructing blocks of code: Initializes all objects, that is, all objects call a block of code. As soon as Object one is established. This code block is called.
Constructor: Initializes the object to which it corresponds. It is targeted.
Person p = new person ();
What does creating an object do in memory?
1: load the Person.class file of the specified location on the hard disk into memory first.
2: When the Main method is executed, the space of the Main method (stack-to-stack) is opened in the stack memory, and a variable p is assigned to the stack area of the main method.
3: creates an entity space in the heap memory and assigns a memory header address value. New
4: Spatial allocation of attributes in the entity space, and default initialization.
5: display initialization of the properties in the space.
6: The Construction code block of the entity is initialized.
7: Call the constructor of the entity to initialize the constructor function. ()
8: The first address is assigned to P, and the P variable refers to the entity. (pointing to the object)
Java Learning (iii) object-oriented