---object-oriented concepts---
1. Process-oriented and object-oriented differences
Process oriented: is a habit of thinking, to solve a problem depends on a method call-the core is the action
Object-oriented: is a more close to the life of the problem-solving thinking habits, solve specific problems by the object of completion-the core is the object
Eg: for the analysis of elephants in refrigerators
Process-oriented thinking habits
Turn on the refrigerator--put it in the fridge--turn off the refrigerator (implemented by 3 actions)
Object-oriented thinking habits
An analysis of the problem involves the object: Elephant freezer (open storage off)
Refrigerator open--refrigerator storage elephant--refrigerator off
2. Object-oriented features
Encapsulating data and methods in an object
Make complex problems simple (oriented to multiple functions-oriented towards an object)
Conversion of roles: implementing functions themselves--invoking the functions of an object
Note: When explaining to others in the future, first say the characteristics, and then to illustrate
3. The general idea of object-oriented problem solving
--analysis of which objects are involved in the problem domain---nouns can be objects in general
Note: Entities-anything that exists in the real world, has both attributes and behavior.
--analyzing the required functions involves two aspects
* This function has been encapsulated in the object, calling the object function directly
* This feature is not encapsulated by the object and encapsulates the feature by creating its own object
Note: The feature should be encapsulated in the appropriate object (principle: Place the object that is most familiar with the feature)
--constant maintenance of relationships between objects
----The relationship between a class and an object----
eg; the production of automobiles in real life
A model car drawing--to generate a specific model of the car
Drawings--the equivalent of a description of something like a car
Specific cars--a concrete product of the description of a thing
Class in--java: Description of a class of things in the Java language-by class
(Attribute-value--member variable, behavior-function-member method) member definition in class
Defining class nature is--defining classes
The difference between a member variable and a local variable
1. The member variable is defined in the class and is valid in the class. Local variables are defined in the method that they belong to, and are valid in the method.
2. The member exists in the heap in the object, the local variable exists in the method in the stack
3. The member variable exists with the object, and the local area exists as the owning region
4. Member variables are initialized by default, local variables are not initialized with default values
Note: Initialization is the initialization of an object's properties
The name of the member variable and the local variable: first in the stack--and then in the heap.
Object in--java: A concrete instance of a class that creates an object from new
Car c = new car ();//Create an Object
Procedure for creating an object: 1. Reference to the Output object 2. Create object in heap (initialize-construct method into stack) 3. The object's address is assigned a reference to the object
C.run (); C.color = "Blue";//Invokes the object's contents through the object name-object. Member
Object. Member method (); method will go into the stack-execution done--out of the stack
Classes are generally divided into: purely descriptive classes and test classes (containing main functions, typically creating and invoking the contents of objects in the class)--the creation of classes is separate from the use of classes
----Object-oriented memory allocation----
1. Heap inside--Save new object (each object contains data in the heap member variable-default initialization)
2. The member method is common to all objects in the class and is loaded into the stack memory only when called
3. The emergence of entities--encapsulating their own unique data,
4. Object: Encapsulates data for a class of things, array: Encapsulates data of the same type
5. Anonymous object: Object with no Name New Car (); It's actually the shorthand way of the object new Car (). run ();
Place of Use: 1. Only one use of the object, such as only a single call method, the anonymous object is not pointed to, call the completion of the variable is garbage, so call a method can be shortened 2. Passing the class type parameter is possible because there are references to receive anonymous objects.
6. Simplification must have advantages and disadvantages
Initialization of---object---
1. Default initialization-Automatic initialization
2. Direct display initialization-class creation is to assign values directly to member variables
3. Static code block initialization--for initializing classes (without objects)
4. Building blocks of code--general initialization for all objects
5. Initialization of the construction method---targeted initialization
Features: function name and class name are the same, no return value type, no return value
function and use time: called once when creating an object, used to initialize the object's member variables
Default initialization: When a generic class is defined, the constructor is not defined and the JVM automatically adds a parameterless constructor, and if the constructor is not added, the constructor is defined in the class in an overloaded manner, and the corresponding constructor is adjusted according to the parameters. As with normal functions, the memory is freed when the call is complete.
Parameter Passing------Java is passed by value
1. The parameter is the basic data type--the value of the variable is passed
2. Parameter is reference type--pass the address pointing to the object
3. When the method call ends, it releases the stack memory it occupies
----Package-----
1. Defining the properties and implementation details of hidden objects provide public access to the outside
2. Advantages: Change isolation, ease of use, code reuse, security
3. Principle: The attribute is generally a private set/get method to provide public
4. Access modifier: Private can only modify members of the class, and can only be accessed directly in this class
5. Writing order of code in a class: member variable construction method member method
---the This keyword----
1. Definition: Represents a reference to the object to which the function belongs---represents this class of object
The simple thing is that the object calls the function where this is, and this represents the object
The constructor method is called by the object, which holds the reference to the object to initialize when it is loaded into memory this = the address of the object
2. When to use: This is used when the function object needs to be used in the function
3. Conditions of Use:
* * local variable and member variable name, this is used to distinguish member variables and parameters this.name = name, when the object calls the member method (if the object data to be accessed)--also holds this reference
* * You can call another constructor in one constructor (the first row must be placed)
---the Static keyword---
1. Used to modify a member, the modified member does not belong to an object
2. The modified members have the following characteristics:
Loaded as the class is loaded, precedes the object's existence
shared by all objects, can be used by the object or can be called directly with the class name
3. When should a static member be defined
Static member variable: All objects of the class this property value share the property value-static only one copy is stored
Static member methods: Methods have no access to the object's unique data-whether it depends on the object's data
If you do not depend on an object, define the function as static
Shared data--defined as static--static variables--typically called by the class name
Unique data-defined as object-specific-member variables (instance variables)
4. The difference between a member variable and a static variable
Two variables have a different life cycle. Member variables and objects are one, static variables and classes are one (life cycle long)
(classes are also stored as objects in memory.)
Called differently: Member variables can only be called by the object, and static variables are typically called with the class name
Different storage places: objects in the member variable heap; Static variables are stored in the method area
5. Precautions
Static methods can only access static members directly
Non-static access to both static and non-static
Static methods can not use this,super and other keywords that represent an object reference static is common
The main function is static-the function is to create the object and invoke the object's contents
Explanation of the 6.main function
Format is fixed
Recognized by the JVM
Main is the name of the function, fixed name identified by the JVM
The main function is called before it executes
String[]args parameter list of main function--string array//JVM parameter new String[0]
7.static member loading process
Test class loading (static non-static separation in the method area-code store)-Main function stack-other function stacks (local variables of the runtime storage method)
8. Static code block
static{} Static is now a modifier that satisfies all static characteristics and executes with the loading of the class
The same class is typically loaded once, only once, and used to initialize a class
Some classes may not need to create objects
Java Basics Summary-object-oriented 1