Understanding what is process-oriented, object-oriented
Process-oriented and object-oriented is a way of thinking in our programming, writing programs.
The process-oriented programming approach is the process of thinking "what should I do" when encountering one thing, and then step-by-step implementation.
L Object-oriented programming, is to meet one thing, think "I should let who do", and then that "who" is the object, how he will do this thing is his own thing, anyway, the last group of objects together can be good on the line.
L Object-oriented thinking mode is a kind of thought which conforms to people's thinking habit.
The more embodiment of the process-oriented thinking is the executor (doing things by himself), and the more embodiment of the object-oriented is the conductor (who directs the object to do things).
L The object-oriented thinking mode simplifies complex problems.
The use of classes and object objects in requirements:
When specific things are described, it is necessary to use these specific things, and Java uses specific things that require the new keyword to create concrete instances of this thing.
1, first in accordance with the terminology of the object of the problem areas
2, the object is described, in fact, in the clear object should have the attributes and functions
3. You can create a specific object of this thing by means of new
4. Call it later functions through the object
The embodiment of the object in the code
Code Demo:
Public class class Name { // can write 0 to n attribute data type variable name 1; data type variable name 2; // You can write 0 to n method modifiers that return a value type method name (parameter) { execute statement;}} Public class Car { String color; int Number ; void run () { + ":" + number );} }
By describing the code, you know that the real meaning of the class is to describe things. Attributes and functions are collectively referred to as members of a thing.
The members of a thing are divided into two types: member properties and member functions.
Member properties are reflected in the code as member variables
Member functionality is reflected in the code as a member method
To create the format of an object:
Class Name Object name = new class name ();
Code Demo:
Public classCardemo { Public Static voidMain (string[] args) {/** Test: The Run method in the car class. */ //1, create an object for car. Give the object a name. Car C =NewCar ();//c is a variable of class type. C points to an object of a specific car type. //2, the function of the object is called through an existing object. Format: Object. Object member; //3, you can assign a value to the property of the object. C.color = "Red"; C.number= 4; C.run (); }}Memory plots for objects
differences between classes and objects:
A class is an abstract description of something, and an object is used to represent an individual of that type of thing in reality.
Class is used to describe common characteristics of multiple objects, which are templates for objects. An object is used to describe an individual in reality, which is an instance of a class.
You can define the properties and behavior of a thing in a class. The object is created using the New keyword by describing the class, and it is possible to invoke the object's specific properties and functions through the object.
differences between local variables and member variables
Difference One: the location of the definition is different
Variables defined in a class are member variables
Variables defined in the method or {} statements are local variables
Difference two: The location in memory is different
Member variables are stored in the object of the heap memory
Local variables are stored in the stack memory method
Difference three: The declaration period is different
Member variables appear in the heap as the object appears and disappear from the heap as the object disappears
Local variables appear in the stack as the method runs, disappearing as the stack of methods pops up
Difference Four: Initialize different
Member variable because in heap memory, all default initialization values
Local variables do not have default initialization values and must be assigned manually before they can be used.
basic types and reference types are passed as parameters
classdemo{ Public Static voidMain (string[] args) {intx = 4; Show (x); System.out.println ("X=" +x); } Public Static voidShowintx) {x= 5; }}//--------------------------------------------------------------------classDemo {intx; Public Static voidMain (string[] args) {Demo d=NewDemo (); D.x= 5; Show (d); System.out.println ("X=" +d.x); } Public Static voidShow (Demo d) {d.x= 6; }}
When a primitive type is passed as a parameter, it is actually copying the value in the basic type variable x space to the Calling method Show (), and when x is copied in the show () method, the X variable is manipulated in the show () method, This will only affect the x in show. When the Show method executes, the program returns to the main method after the stack, and the X value in the main method is the original value.
When a reference variable is passed as a parameter, it is actually a copy of the memory address (Reference) in the reference variable space to the D reference variable passed to the Show method. At this point there will be two references pointing to the same object in the heap. When you execute d.x=6 in the Show method, the objects in the heap are found based on the reference held by D, and the value of its X property is changed to the 6.show method stack.
Since the two references point to the same object, no matter which reference changes the value in the referenced object, the other reference is used again as the changed value.
java-Basics-Object oriented