1. Classes and objects
1.1 understanding of classes and objects [understanding]
Everything that exists objectively is an object, so we often say that everything is an object.
- class
- Class understanding
- Class is an abstraction of a class of things with common attributes and behaviors in real life
- A class is the data type of an object. A class is a collection of objects with the same properties and behavior
- Simple understanding: class is a description of real things
- Composition of classes
- Attribute: refers to the characteristics of things, such as mobile phone things (brand, price, size)
- Behavior: refers to the operations that things can perform, such as mobile phone things (making phone calls and sending text messages)
- Class understanding
- Relationship between classes and objects
- Class: class is the abstraction of a class of things with common attributes and behaviors in real life
- Object: a real entity that can be seen and touched
- Simple understanding: class is a description of things, and the object is a concrete thing
1.2 definition of category [application]
Class consists of two parts: attribute and behavior
- Attribute: reflected in the class through member variables (variables outside the methods in the class)
- Behavior: reflected in the class through member methods (compared with the previous methods, just remove the static keyword)
Class definition steps:
① Define class
② Write the member variable of the class
③ Write member methods of classes
public class Class name { // Member variable Data type of variable 1; Data type of variable 2; ... // Member method Method 1; Method 2; }
Example code:
/* Mobile phone category: Class name: Mobile (Phone) Member variables: Brand Price Member method: Call Send SMS (sendMessage) */ public class Phone { //Member variable String brand; int price; //Member method public void call() { System.out.println("phone"); } public void sendMessage() { System.out.println("send message"); } }
1.3 use of objects [ application ]
- Format for creating objects:
- Class name object name = new class name ();
- Format of calling member:
- Object name. Member variable
- Object name. Member method ();
- Sample code
/* create object Format: class name object name = new class name (); Example: Phone p = new Phone(); Use object 1: Using member variables Format: object name. Variable name Example: p.brand 2: Using member methods Format: object name. Method name () Example: p.call() */ public class PhoneDemo { public static void main(String[] args) { //create object Phone p = new Phone(); //Using member variables System.out.println(p.brand); System.out.println(p.price); p.brand = "millet"; p.price = 2999; System.out.println(p.brand); System.out.println(p.price); //Using member methods p.call(); p.sendMessage(); } }
1.4 Student object - exercise [application]
- Requirements: first define a student class, and then define a student test class. In the student test class, the use of member variables and member methods is completed through objects
- analysis:
- Member variables: name, age
- Member method: study, do homework
- Example code:
class Student { //Member variable String name; int age; //Member method public void study() { System.out.println("study hard and make progress every day"); } public void doHomework() { System.out.println("The keyboard is broken, and the monthly salary is more than 10000"); } } /* Student testing */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Use object System.out.println(s.name + "," + s.age); s.name = "Lin Qingxia"; s.age = 30; System.out.println(s.name + "," + s.age); s.study(); s.doHomework(); } }
2. Object memory diagram
2.1 single object memory diagram [understanding]
- Member variable usage process
- Member method call procedure
2.2 multiple object memory diagram [understanding]
- Member variable usage process
- Member method call procedure
-
Summary:
Multiple objects have different memory partitions in heap memory. Member variables are stored in their respective memory areas. Member methods are shared by multiple objects
2.3 multiple objects point to the same memory graph [understand]
[external chain picture transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-vXKwtxtd-1631716650028)(E:\BaiduNetdiskDownload \ employment class 2.1 note source code - compressed version \ stage 1: membership Version (2.1)]-Java foundation and advanced \ stage 1: membership Version (2.1)-Java foundation and advanced \ 1-02 employment class (2.1) - object-oriented encapsulation [section 1 ~ section 2] \ notes \ img(1).png)
-
summary
When references to multiple objects point to the same memory space (the address values recorded by variables are the same)
As long as any object modifies the data in memory, then, no matter which object is used for data acquisition, it is the modified data.
3. Member variables and local variables
3.1 difference between member variables and local variables [understanding]
- Different positions in the class: member variable (outside the method in the class) local variable (inside the method or on the method declaration)
- Different locations in memory: member variables (heap memory) local variables (stack memory)
- Different life cycles: member variables (exist with the existence of the object and disappear with the disappearance of the object) local variables (exist with the call of the method and disappear after the call of the method)
- Different initialization values: member variable (with default initialization value) local variable (without default initialization value, it must be defined before assignment)
4. Packaging
4.1 private keyword [understanding]
private is a modifier that can be used to modify members (member variables, member methods)
-
Members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, corresponding operations are 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
-
Example code:
/* Student class */ class Student { //Member variable String name; private int age; //Provide get/set methods public void setAge(int a) { if(a<0 || a>120) { System.out.println("You gave the wrong age"); } else { age = a; } } public int getAge() { return age; } //Member method public void show() { System.out.println(name + "," + age); } } /* Student testing */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Assign values to member variables s.name = "Lin Qingxia"; s.setAge(30); //Call the show method s.show(); } }
4.2 use of private [ application ]
-
Requirements: define a standard student class, require name and age to be decorated with private, provide set and get methods and show methods to display data, create and use objects in the test class, and finally output them on the console Lin Qingxia, 30
-
Example code:
/* Student class */ class Student { //Member variable private String name; private int age; //get/set method 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); } } /* Student testing */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Assign values to member variables using the set method s.setName("Lin Qingxia"); s.setAge(30); s.show(); //Use the get method to get the value of the member variable System.out.println(s.getName() + "---" + s.getAge()); System.out.println(s.getName() + "," + s.getAge()); } }
4.3 this keyword [ application ]
- The variable modified by this is used to refer to member variables. Its main function is to distinguish the problem of duplicate names of local variables and member variables
- If the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter, not the member variable
- The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable
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); } }
4.4 principle of this memory [understanding]
-
This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.
-
Example code:
public class StudentDemo { public static void main(String[] args) { Student s1 = new Student(); s1.setName("Lin Qingxia"); Student s2 = new Student(); s2.setName("Zhang Manyu"); } }
-
Illustration:
4.5 packaging idea [understanding]
- Packaging overview
Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
It is the simulation of the objective world by object-oriented programming language. In the objective world, the member variables are hidden inside the object, and the outside world can not be operated directly - Encapsulation principle
Some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class
The member variable private provides the corresponding getXxx()/setXxx() methods - Packaging benefits
Methods are used to control the operation of member variables, which improves the security of the code
The code is encapsulated by method, which improves the reusability of the code
5. Construction method
5.1 overview of construction method [understanding]
Construction method is a special method
-
Function: create object Student stu = new Student();
-
Format:
public class class name{
Modifier class name (parameter){
}
}
-
Function: it mainly completes the initialization of object data
-
Example code:
class Student { private String name; private int age; //Construction method public Student() { System.out.println("Nonparametric construction method"); } public void show() { System.out.println(name + "," + age); } } /* Test class */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); s.show(); } }
5.2 precautions for construction method [understanding]
- Creation of construction method
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
- Overloading of construction methods
If you customize the construction method with parameters and use the nonparametric construction method, you must write another nonparametric construction method
- Recommended usage
Whether used or not, the parameterless construction method is written manually
- Important functions!
You can initialize member variables using a parameterized construct
- Sample code
/* Student class */ class Student { private String name; private int age; public Student() {} public Student(String name) { this.name = name; } public Student(int age) { this.age = age; } public Student(String name,int age) { this.name = name; this.age = age; } public void show() { System.out.println(name + "," + age); } } /* Test class */ public class StudentDemo { public static void main(String[] args) { //create object Student s1 = new Student(); s1.show(); //public Student(String name) Student s2 = new Student("Lin Qingxia"); s2.show(); //public Student(int age) Student s3 = new Student(30); s3.show(); //public Student(String name,int age) Student s4 = new Student("Lin Qingxia",30); s4.show(); } }
5.3 standard production [ application ]
- Requirements: define a standard student class, which requires the creation of objects by using null parameter and parametric construction methods respectively. The objects created by null parameter are assigned by setXxx, the objects created by parametric are assigned directly, and the data is displayed by show method.
- Example code:
class Student { //Member variable private String name; private int age; //Construction method public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } //Member method 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); } } /* There are two ways to create an object and assign values to its member variables 1:After the object is created by the parameterless constructor, setXxx() is used for assignment 2:Use the construction method with parameters to directly create objects with attribute values */ public class StudentDemo { public static void main(String[] args) { //After the object is created by the parameterless constructor, setXxx() is used for assignment Student s1 = new Student(); s1.setName("Lin Qingxia"); s1.setAge(30); s1.show(); //Use the construction method with parameters to directly create objects with attribute values Student s2 = new Student("Lin Qingxia",30); s2.show(); } }
public int getAge() { return age; } public void show() { System.out.println(name + "," + age); }
}
/*
There are two ways to create an object and assign values to its member variables
1: After the object is created by the parameterless constructor, setXxx() is used for assignment
2: Use the construction method with parameters to directly create objects with attribute values
*/
public class StudentDemo {
public static void main(String[] args) {
//After the object is created by the parameterless constructor, setXxx() is used for assignment
Student s1 = new Student();
s1.setName("Lin Qingxia");
s1.setAge(30);
s1.show();
//Use the construction method with parameters to directly create objects with attribute values Student s2 = new Student("Lin Qingxia",30); s2.show(); }
}