[Classes and Objects, Encapsulation, Construction Method]
Overview of Object-Oriented Thought
1. Process-oriented and object-oriented
- Process Oriented: Emphasizing Process (emphasizing every step of doing something)
- Object-Oriented: Do one thing, not do it yourself, but use objects to accomplish it.
Give an example
-
Procedure-oriented: Take a basin, put clothes, put laundry powder, pour water, bubble, rub, rinse, wring dry, and air it.
-
Object-Oriented: Put clothes in the washing machine, put laundry powder, press the switch, and air them.
Call my girlfriend to wash it for me
II. Categories and Objects
1. What are classes and objects?
- Class: A set of attributes and behaviors of a class of things.
- Object: The concrete embodiment of this kind of thing.
2. How to define classes
- Class: A class defined in Java using class.
- Attribute (member variable): The format of definition is the same as that of previous variables, except that the location has changed, outside the methods in the class.
- Behavior (member method): Definition format is basically the same as before, except static is removed.
public class Person { // Extra-method in class // Full name String name; // Age int age; // Method // Having dinner public void eat() { System.out.println("People want to eat"); } // Sleep public void sleep() { System.out.println("People want to sleep"); } }
3. How to create and use objects
Create and use objects in the main method of the test class.
(1) Creating objects
// Class name object name = new class name (); // A Person object was created with the object name p Person p = new Person();
(2) Use objects
// Use attributes: object names. attribute names p.name; // Use behavior: object name. method name ()
If you want to use the content of another class in one class, you need to create objects first.
/** * Test class */ public class PersonTest { public static void main(String[] args) { // Write code to simulate various scenarios. // To test whether the content in the functional class is correct // A Person object was created with the object name p Person p = new Person(); // Object name. Attribute name p.name = "Yang Chao"; p.age = 20; // Printing System.out.println("The name is: " + p.name + ", Age is: " + p.age); p.eat(); p.sleep(8); } }
4. Membership variables and local variables
[External Link Picture Transfer Failure (img-8gJ7xiRe-1566980415899)(assets/1563502911445.png)]
Differences between member variables and local variables
describe | Different Positions | Scope (in brackets) | Default values are different |
---|---|---|---|
local variable | Method or method declaration (parameter list) | In the method | No default value |
Membership variables | Extra-method in class | In the class where you belong | Have default values |
Local variables must be assigned before they are used
data type | Default values |
---|---|
Integer (byte, short, int, long) | 0 |
Floating point type (float, double) | 0.0 |
Boolean | false |
Character (char) | '\u0000' |
Reference data type | null |
3. Create an object's memory map
IV. Packaging [Key Points]
1. Principles and formats of encapsulation
private: Can modify member variables and member methods, role: privately modified content, can only be accessed in this class.
- Hidden attributes: Decorate attributes with private
- Providing public access to the outside world: get/set method
2. Optimize 1- this
-
In the set method, member variables and local variables have the same name.
- At this point, to follow the principle of proximity, there appears an assignment statement, which becomes: local variables are assigned to local variables.
age = age; name = name;
this: What problems can be solved?
- When the member variable and the local variable have the same name, the member variable is obtained by using this. variable name.
3. Optimizing 2-Construction Method
- Objective: To assign values to member variables while creating objects.
Construction method
- Format:
Modifier construction method name (parameter list) {} - No return value type, not even void - The name and class name of the constructor are the same // Spatial-parametric structure/parametric-free structure public class name (){ } // parametric structure/full parametric structure/full parametric structure The public class name (parameter list) {// has parameters in parentheses, and the parameters are written according to all member variables in the class. this. Property name = the name of a local variable in the parameter list; }
Effect
- Used to create objects (when creating objects, the constructor must be called)
- Initialization (assignment) of members in a class
Matters needing attention:
- If no constructor is given manually in the class, the system defaults to providing empty parameter constructs.
- If any constructor is given manually in the class, the system will not provide this empty parameter construct by default.
case
Student category
package com.demo.practice.homework6; public class Student { // Student // Attributes: chinese, math, english // Behavior: getSum() private int chinese; private int math; private int english; public Student(){ } public Student(int chinese,int math,int english){ this.chinese = chinese; this.math = math; this.english = english; } public void setChinese(int chinese){ this.chinese = chinese; } public void setMath(int math){ this.math = math; } public void setEnglish(int english){ this.english = english; } public int getChinese(){ return chinese; } public int getMath(){ return math; } public int getEnglish(){ return english; } public int getSum(){ return chinese+math+english; } }
Test class
package com.demo.practice.homework6; public class Test { public static void main(String[] args) { Student s = new Student(70,60,90); System.out.println("chinese:"+s.getChinese()+" math:"+s.getMath()+" english:"+s.getEnglish()); System.out.println("Total score:"+s.getSum()); } }
Summary of Overall Knowledge Points
ps: !!!
It's also convenient to upload photos. If only XMind could be supported, I think this mind mapping software is very good. The idea can be more clear. The general knowledge system after summarizing is still very structured.