A new understanding of object-oriented
Process oriented programming: (1) clear and simple steps (2) suitable for dealing with some relatively simple problems
Object oriented programming: (1) the thinking mode of classification. When thinking about the problem, we will first solve what classifications the problem needs, and then think about these classifications separately. Finally, process oriented thinking about the details under a certain classification; (2) Object oriented is suitable for dealing with complex problems and problems requiring multi person cooperation.
Conclusion: for describing complex transactions, in order to grasp them macroscopically and analyze them reasonably as a whole, we need to use object-oriented thinking to analyze the whole process. However, specific to micro operation, it still needs process oriented thinking to deal with it.
Definition: object oriented programming (OOP)
Essence: organize code in the form of classes and organize (encapsulate) data in the form of objects.
Thought: (1) abstract thought (2) three characteristics -- encapsulation, inheritance and polymorphism;
From the perspective of epistemology, there are objects before classes. Objects are concrete things. Class is abstract, which is the abstraction of objects.
From the perspective of code operation, there are classes before objects. A class is a template for an object.
II. Method review and deepening
Two parts: definition of method; Method call
1 definition of method
(1) Structure: Modifier return value type method name (...){ // method Return return value }
(2) Modifier: shape, such as public, static, etc
(3) Return type: determines the type of return value of the method: basic data type, array, other structure, empty, etc
(4) The difference between break and return
return ends the method and returns a result
break end loop or switch loop
(5) Method name: hump principle
(6) Parameter list: the parameter list entered into the method, for example, public int max (int a, int b)
Use of variable length parameter public int max (int... x)
(7) Exception throw: not explained in detail for the time being. There have been array out of bounds exceptions before.
2. Method call
(1) Static method: class of static. Static method is called directly by static
(2) Non static method: a class without static needs to be instantiated before calling
Create a student class to call its static class methods and non static class methods, and call the static class methods inside the class itself;
package OOP; public class Demo02 { public static void main(String[] args) { //Static method static direct call Student.say(); //Non static methods cannot be called directly //Student.study(); / / an error will be reported //The call needs to be instantiated Student student2 = new Student(); student2.study(); } //Other examples of method calls: static //When both method a and method b are static, they can call each other. When one of them is not static, the other cannot call it! public static void a() { b(); //c(); / / an error will be reported } public static void b() { System.out.println("hello,I am b function"); } public void c() { System.out.println("C"); } }
Student class to be called:
package OOP; public class Student { public static void say() { System.out.println("The student spoke"); } public void study() { System.out.println("The students have studied"); } }
(3) Formal and argument
Formal parameter: when defining a method, it is used to describe the parameters of the passed in parameter type
Argument: the parameter passed into the method when the method is called
In the following code, int a and int b in Max method are formal parameters; when calling Max method, i and j in max(i,j) are actual parameters.
public class Demo03 { public static void main(String[] args) { int i = 1, j = 2; int ans = max(i,j);//The i and j passed in are the arguments System.out.println(ans); } public static int max(int a, int b){ //The specified incoming data types a and b are formal parameters return a>b?a:b; } }
(4) Value passing and reference passing
Java is value passing. What you get is the return value.
Reference passing is similar to pointer passing in C, that is, the address is passed. When modifying, it will be modified directly in place.
Examples of value transfer:
//Example of value passing int a = 1; System.out.println("The current value is:"+a); Demo04 demo04 = new Demo04(); demo04.fun1(a); System.out.println("The current value is:"+a);
public class Demo04 { public void fun1(int a){ a = a+1; System.out.println("stay demo04 In the method of a The value of has been changed to:"+a); System.out.println("however demo04 The method of has no return value!"); return; } }
Examples of reference passing
//Examples of reference passing Person person = new Person(); System.out.println("change person of name Value before property:"+person.name); change(person);//Change the name attribute value of person System.out.println("change person of name Value after property:"+person.name);
//Call method of reference passing example public static void change(Person person) { person.name = "yuan";
//Class that references the call passed class Person{ String name; }
(5) this keyword
It refers to the member variables in the access class, which are used to distinguish between member variables and local variables (duplicate name problem)
Three object creation analysis
(1) Relationship between class and object:
Class is an abstract data structure. It is the overall description / definition of a transaction, but it can not represent a specific thing.
Objects are concrete instances of abstract concepts.
(2) Creating and initializing objects
Use the new keyword to create an object. When using the new keyword to create an object, in addition to allocating memory space, the created object will be initialized by default and the constructor in the class will be called.
package OOP.Demo_main; public class Student { //attribute String name; int age; //method public void study() { System.out.println("I'm learning"); } }
package OOP.Demo_main; //There is only one main method in a project public class Demo01 { public static void main(String[] args) { //Class Student student = new Student(); // A student object is a concrete instance of a student class student.study(); //After instantiation, the object will be initialized with null string and 0 int System.out.println(student.name + " " + student.age); student.age = 15; student.name = "xian"; System.out.println(student.name + " " + student.age); }
(3) Constructor
The constructor in a class, also known as a constructor, must be called when creating an object, and the constructor has two characteristics: 1. It must have the same name as the class; 2. It must have no return value and cannot write void
A class will have a constructor even if it doesn't write anything;
public Student(){ }
With the explicit constructor, we can initialize the value of the object
//The explicit parameterless constructor, with the default name value of yuan2, will get the object with the name of yuan2 when instantiating this class public Student(){ this.name = "yuan2"; }
When defining a parameterized construct, a nonparametric constructor must display the definition as follows
//When constructing a parametric constructor, there must be a display constructor public Student(){ } public Student(String name){ this.name = name; }
Constructor summary: 1 is the same as the class name; 2 has no return value
Function: 1. The essence of new is to call the constructor. 2. Initialize the value of the object
Note: after defining a parameterized construct, if you want to use a parameterless construct, it will be displayed to define a parameterless construct
Shortcut key: alt+insert key to quickly build a parametric or nonparametric constructor
(4) Memory analysis
After class 1 is created, it is placed in the method area
2 when calling, first press the main method into the bottom of the stack (the memory of the heap is not opened)
3 when calling a method, as a reference variable name, it is first pushed into the stack, and then the memory space is opened up in the heap. The reference variable name points to the memory address of the block.
4 at the end of the call, the memory is recovered and pop out of the stack.
Static: it is stored in the static method area and loaded with the class so that all objects can call it.