7.1 process oriented and object oriented
Process oriented language: c language (solve problems by yourself)
Object oriented: c + +, java, python (others solve problems)
In the processing of anything, it is generally process-oriented, but the idea of object-oriented may be used in the middle. Process oriented is the lowest level, and object-oriented is based on process oriented!
7.2 relationship between class and object
Object oriented programming is programming with objects. Object represents an entity that can be clearly identified in the real world.
• the state of an object, also known as a feature or attribute, is represented by a data field with a current value.
• - the behavior of an object is defined by methods. One way to call an object is to ask the object to complete an action.
Use a common class to define objects of the same type. A class is a template, blueprint or contract used to define what the data field of an object is and what the method does. An object is an instance of a class. You can create multiple instances from a class. The process of creating an instance is called instantiation. Objects and instances are often interchangeable. The relationship between classes and objects is similar to the relationship between apple pie recipe and apple pie. You can make as many apple pies as you like with one recipe.
7.3 code implementation and memory diagram
/* At present, this class is mainly used to describe things Generally, the classes used to describe things are collectively referred to as entity classes Entity classes generally have no main function Properties: brand colour speed weight Vehicle start state Behavior: start-up close accelerate brake */ public class Car{ String band; String color; int speed; int weight; boolean statues; public void startEngine() { if (statues) { System.out.println("The car engine is off...."); statues = false; } else { System.out.println("The car engine starts...."); statues = true; } } public void shutDownEngine() { if (statues) { System.out.println("The car engine is off...."); statues = false; } else { System.out.println("The car engine starts...."); statues = true; } } public void addSpeed() { if (statues) { System.out.println("Car acceleration...."); speed += 10; } else { System.out.println("The car engine has not started yet...."); } } public void jianSpeed() { if (statues) { System.out.println("Car deceleration...."); speed -= 10; if (speed <= 0) { speed = 0; } } else { System.out.println("The car engine has not started yet...."); } } } Memory diagram of the above code javac Sample.java Compile the source code of the main class, but it will be involved in the main class Car This class also Car This class It also mutated, so two bytecode files were finally generated, Sample.class,Car.class java Sample function Sample This bytecode file, why not run it Car What about bytecode files? because Car Not the Lord Class, so there is no main function, so it can't run, Sample Is the main class. There is a main function in it, so it runs Sample This bytecode file How does it work? First load the relevant bytecode file into the JVM In, JVM It runs in computer memory, so it can also be used So it is thought that the bytecode file is loaded into the local memory of the computer. Where is it loaded? Load in JVM One of them is In the memory of the method area according to java Sample For this instruction, go to the method area Sample The main function in bytecode reads the function frame of the main function And load into stack memory ,Start executing main function Because the main function has formal parameters, this formal parameter String[] args ,The formal parameter of any function is the Local variable under function! /* Sample This class does not describe the content of things It is only used to run programs. Generally, there are main functions This class is called the master class */ public class Sample { public static void main(String[] args) { //Create an object of Car class. The name of the object is c1 Car c1 = new Car(); //Take a look at the properties of c1 System.out.println(c1.band); System.out.println(c1.color); System.out.println(c1.speed); System.out.println(c1.weight); System.out.println(c1.statues); c1.band = "audi"; c1.color = "black"; c1.speed = 0; c1.weight = 4; c1.statues = false; System.out.println(c1.band); System.out.println(c1.color); System.out.println(c1.speed); System.out.println(c1.weight); System.out.println(c1.statues); c1.startEngine(); c1.addSpeed(); c1.addSpeed(); c1.addSpeed(); System.out.println(c1.speed); c1.jianSpeed(); c1.jianSpeed(); System.out.println(c1.speed); c1.shutDownEngine(); Car c2 = new Car(); c2.startEngine(); c2.shutDownEngine(); } }
Memory diagram
7.4 differences between local variables and member variables
Belonging space
Member variable: in heap memory, the space to which the object belongs
Local variable: in the stack memory, the space to which the function belongs
life cycle
Member variable: it is created with the creation of the object and dies with the death of the object
Local variable: it is created as the function moves into the stack and disappears as the function bounces into the stack
initialization
Member variables: default initialization - explicit initialization - targeted initialization
Local variable: if there is no initialized value, it cannot be called later
Scope
Member variable: it is global inside this class (static cannot call non static)
Local variable: the premise is that in the function, it is within the nearest pair of braces
If a local variable and a class variable have the same name, the local variable takes precedence, and the class variable with the same name will be hidden. For example, in the following program, x is defined as an instance variable and a local variable in the method.
public class F { private int x = 0; private int y = 0; public F() { } public void p() { int x - 1; System.out.println("x = " + x); System.out.println("y = " + y); }
7.5 private keyword and accessor modifier
The English meaning of private is "private", so we can privatize member variables by using the private keyword, allowing only internal access. Member variables cannot be directly modified by the outside world. For member variables, the value can be modified, but it cannot be without logic and conditions. Therefore, for objects, there should be corresponding functions to verify the legitimacy of parameters transmitted from the outside world! This function is called the modifier setXXX() and provides the accessor getXXX().
7.6 constructor and memory diagram
Constructor: it is a function called when creating an object. One is to perform display initialization + targeted initialization for the object, and the other is to inherit relevant contents.
Construction method is a special method. They have the following three particularities:
• the constructor must have the same name as the class.
• constructor has no return value type, not even void.
• the constructor is called when an object is created using the new operator. Constructors are used to initialize objects.
A constructor has exactly the same name as the class that defines it. Like all other methods, constructors can also be overloaded (that is, there can be multiple constructors with the same name, but they should have different signatures), which makes it easier to construct objects with different initial data values.
public class DemoA { public static void main(String[] args) { //StackOverflowError stack memory overflow error A a = new A(); System.out.println(a == a.a); System.out.println(a.a == a.a.a); } } class A { A a = new A(); }
7.7 static member and memory diagram
Static variables are shared by all objects in the class. Static methods cannot access instance members in a class.
Static variables store variable values in a common memory address. Because it is a public address, if an object modifies the value of a static variable, all objects of the same class will be affected. Java supports static methods and static variables. You can call static methods without creating an instance of a class.
public class DemoB { public static void main(String[] args) { B b = new B(); System.out.println(b == b.b); //0x456 0x123 false System.out.println(b.b == b.b.b);//0x123 0x123 true } } class B { static B b = new B(); }