Pop (procedure oriented programming) is a process centered programming idea, and each step of the function is realized by itself.
Object oriented programming (OOP) is an object-centered programming idea, which realizes specific functions by commanding objects.
1. Classes and objects
Class is an abstraction of a class of things with common attributes and behaviors in real life.
Objects are real entities that can be seen and touched (all things are objects).
A class is a description of an object, and an object is an entity of a class.
Class composition:
- Attribute: various characteristics of things are reflected in the code through member variables (variables outside the methods in the class)
- Behavior: the existing function of things (what can be done) is reflected in the code through the member method (compared with the previous method, just remove the static keyword)
Example: Student.java
package com.qdu.object1; public class Student { String name; int age; public void study() { System.out.println("study"); } }
Create object: class name object name = new class name ();
Use member variable: object name. Member variable
Use member method: object name. Member method ();
Example: TestStudent.java
package com.qdu.object1; public class TestStudent { public static void main(String[] args) { Student stu = new Student(); System.out.println(stu.name); // null System.out.println(stu.age); // 0 stu.name = "Zhang San"; stu.age = 23; System.out.println(stu.name); // Zhang San System.out.println(stu.age); // 23 stu.study(); System.out.println(stu); // com.qdu.object1.Student@b4c966a // @: separator // b4c966a: hexadecimal memory address // com.qdu.object1.Student: full class name (i.e. package name + class name) } }
Example: define a mobile phone class, and then define a mobile phone test class. In the mobile phone test class, the use of member variables and member methods is completed through objects.
Phone.java
package com.qdu.test1; public class Phone { String brand; // brand int price; // Price public void call(String name) { System.out.println("to"+name+"phone"); } public void sendMessage() { System.out.println("Mass texting"); } }
TestPhone.java
package com.qdu.test1; public class TestPhone { public static void main(String[] args) { Phone p = new Phone(); p.brand = "rice"; p.price = 2999; System.out.println(p.brand + "..." + p.price); p.call("A Qiang"); p.sendMessage(); } }
2. Single object memory diagram
3. Two object memory diagrams
4. Two references point to the same object memory map
Note: in heap memory, when the address generated by the object or array cannot be found in any way, it will be judged as "garbage" in memory, and the garbage will be automatically cleaned up by the Java garbage collector when it is idle.
5. Member variables and local variables
Member variable: a variable outside a method in a class.
Local variable: a variable in a method.
6.private keyword
Private is a permission modifier. Private can modify members (member variables and member methods). Members decorated with private can only be accessed in this class.
If the private modified member variable needs to be used by other classes, corresponding operations shall be provided:
- The get variable name () method is provided to obtain the value of the member variable. The method is decorated with public
- The set variable name (parameter) method is provided to set the value of the member variable. The method is decorated with public
Student.java
package com.qdu.test2; public class Student { private String name; private int age; public void setName(String n) { name = n; } public String getName() { return name; } public void setAge(int a) { age = a; } public int getAge() { return age; } public void show() { System.out.println(name + "..." + age); } }
TestStudent.java
package com.qdu.test2; public class TestStudent { public static void main(String[] args) { Student stu = new Student(); stu.setName("Zhang San"); stu.setAge(23); System.out.println(stu.getName()); System.out.println(stu.getAge()); int age = stu.getAge(); for(int i = 1; i <= age; i++){ System.out.println("Happy birthday!"); } stu.show(); // Just show the data } }
7.this keyword
If local variables and member variables have the same name, Java uses the proximity principle.
Function of this keyword: you can call members of this class (member variables and member methods) to solve the problem of duplicate names of local variables and member variables.
package com.qdu.test2; public class Student { private String name; private int age; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void show(){ System.out.println(name + "..." + age); } }
package com.qdu.test2; public class Test2Student { public static void main(String[] args) { Student s1 = new Student(); s1.setName("Zhang San"); s1.setAge(23); s1.show(); } }
When using member variables, the system will add this. Automatically by default.
this represents the object reference of the class. this represents the object to which the method is called.
Example:
package com.qdu.mthis; public class Student { private String name; public void setName(String name){ System.out.println("[Print in method this]"); System.out.println(this); this.name = name; } }
package com.qdu.mthis; public class TestStudent { public static void main(String[] args) { Student s1 = new Student(); System.out.println("[Print object name]-->s1"); // com.qdu.mthis.Student@b4c966a System.out.println(s1); // com.qdu.mthis.Student@b4c966a s1.setName("Zhang San"); System.out.println("----------------------"); Student s2 = new Student(); System.out.println("[Print object name]-->s2"); // com.qdu.mthis.Student@2f4d3709 System.out.println(s2); // com.qdu.mthis.Student@2f4d3709 s2.setName("Li Si"); } }
8. Principle of this memory
9. Packaging
Three characteristics of object-oriented: encapsulation, inheritance and polymorphism.
Hide implementation details and only expose public access methods.
Q: does encapsulation mean private?
A: private is just an embodiment of encapsulation.
Common manifestations of packaging:
- Private member variable, providing setXxx and getXxx methods
- Extracting code into methods is an encapsulation of code
- Extracting attributes into classes is an encapsulation of data
Benefits of encapsulation:
- Improved code security
- It improves the reusability of the code
10. Construction method
Construction method, that is, the method called when building / creating objects.
Format:
- The method name is the same as the class name, and the case should be consistent.
- There is no return value type, not even void.
- There is no specific return value (the result data cannot be brought back by return).
Execution time:
- Called when creating an object. Each time an object is created, the construction method will be executed.
- Constructor cannot be called manually.
public class Student { //Construction method public Student() { System.out.println("I am Student Class construction method"); } } public class TestStudent { public static void main(String[] args) { //create object Student stu1 = new Student(); // Output: I am the constructor of the Student class Student stu2 = new Student(); // Output: I am the constructor of the Student class Student stu3 = new Student(); // Output: I am the constructor of the Student class stu1.Student(); // An error is reported. The constructor cannot be called manually } }
The constructor can be used to initialize the data (properties) of the object.
Nonparametric construction method: the age of all Student objects is 18, which is unreasonable.
Construction method with parameters:
If no construction method is defined, the system will give a default parameterless construction method.
If a construction method is defined, the system will no longer provide the default construction method.
If you customize the construction method with parameters and use the nonparametric construction method, you must write another nonparametric construction method.
Suggestion: whether it is used or not, manually write the nonparametric construction method and the parametric construction method.
package com.qdu.constructor; public class Student { private String name; private int age; public Student(){} public Student(String name, int age) { this.name = name; this.age = age; System.out.println("I am Student Class construction method"); } public void show() { System.out.println(name + "..." + age); } }
package com.qdu.constructor; public class TestStudent { public static void main(String[] args) { Student stu1 = new Student("Zhang San",23); stu1.show(); Student stu2 = new Student(); } }
Example: JavaBean class, used to encapsulate data.
package com.qdu.test3; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void show(){ System.out.println(name + "..." + age); } }
package com.qdu.test3; public class TestStudent { public static void main(String[] args) { // 1. Create an object with the nonparametric construction method and assign a value to the member variable through the setXxx method Student stu1 = new Student(); stu1.setName("Zhang San"); stu1.setAge(23); stu1.show(); // 2. Directly assign values to attributes through the construction method with parameters Student stu2 = new Student("Li Si",24); stu2.show(); } }
Quickly generate default nonparametric construction methods and parametric construction methods in IDEA:
Quickly generate setXxx and getXxx methods in IDEA: