1. Classes and objects
- Understanding of classes and objects
All things that exist objectively are objects, so we often say that all things are objects.
-
class
Class understanding- Class is the abstraction of a kind of things with common properties and behaviors in real life
- A class is a data type of an object, and a class is a collection of objects with the same properties and behaviors
- Simple understanding: class is a description of real things
Class composition
- Attribute: refers to the characteristics of things, such as: Mobile things (brand, price, size)
- Behavior: refers to the operation that things can perform, such as: Mobile things (making phone calls, texting)
Relationship between class and object
- Class: class is the abstraction of a kind of things with common properties and behaviors in real life
- Object: the real entity that can be seen and touched
- Simple understanding: class is a description of things, and the object is the specific existence of things
- Class definition
Class consists of two parts: property and behavior
- Attribute: in a class, it is represented by member variables (variables other than methods in the class)
- Behavior: it is embodied in the class by member methods (compared with the previous methods, the static keyword can be removed)
- To define a class:
Definition class
② Writing member variables of a class
③ Writing member methods of a class
public class name{ //Member variable Data type variable 1 of variable 1; Data type variable 2 of variable 2; ... //Member method Method 1; Method 2; }
- Example code:
/* Mobile phone category: Class name: mobile phone Member variable: brand price Member method: call and send message */ public class Phone { //Member variables String brand; int price; //Member method public void call() { System.out.println("Phone"); } public void sendMessage() { System.out.println("Send message"); } }
- Use of objects
- Create the format of the object:
Class name object name = new class name (); - Format of calling member:
Object name. Member variable
Object name. Member method (); - Example code:
/* create object Format: class name object name = new class name(); Example: Phone p = new Phone(); Use object 1: Use member variables Format: object name. Variable name Example: p.brand 2: Use member method Format: object name. Method name () Example: p.call() */ public class PhoneDemo { public static void main(String[] args) { //create object Phone p = new Phone(); //Use 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); //Use member method p.call(); p.sendMessage(); } }
- Student object - Exercise
- Demand:
First, a student class is defined, then a student test class is defined. In the student test class, member variables and member methods are used through objects - Analysis:
Member variables: name, age
Member method: study, do homework - Example code:
class Student { //Member variables 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 over ten thousand"); } } /* Student test */ 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 = "lisa"; s.age = 20; System.out.println(s.name + "," + s.age); s.study(); s.doHomework(); } }
- Conclusion:
Multiple objects in heap memory have different memory partitions, member variables are stored in their respective memory areas, and member methods share the same share of multiple objects
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.
2. Member variable and local variable
- 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 variable (heap memory) local variable (stack memory)
- Different life cycle: member variable (exists with the existence of the object, disappears with the disappearance of the object) local variable (exists with the invocation of the method, disappears with the completion of the invocation of the method)
- Initialization value is different: member variable (with default initialization value) local variable (without default initialization value, it must be defined before assignment)
3. encapsulation
- private keyword
private is a modifier that can be used to modify members (member variables, member methods)
-
Members decorated with private can only be accessed in this class. For member variables decorated with private, if they need to be used by other classes, corresponding operations are provided
-
"Get variable name ()" method is provided to get the value of member variable. The method is decorated with public
-
Provide the "set variable name (parameter)" method, which is used to set the value of member variables. The method is decorated with public
-
Example code:
/* Student class */ class Student { //Member variables String name; private int age; //Provide get/set method public void setAge(int a) { if(a<0 || a>120) { System.out.println("You gave me the wrong age"); } else { age = a; } } public int getAge() { return age; } //Member method public void show() { System.out.println(name + "," + age); } } /* Student test */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Assign value to member variable s.name = "lisa"; s.setAge(20); //Call show method s.show(); } }
- Use of private
- Demand:
Define the standard student class, require name and age to use private decoration, and provide set and get methods and show methods to display data easily, test the objects in the class and use them, and finally the console outputs lisa,20 - Example code:
/* Student class */ class Student { //Member variables 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 test */ public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); //Assign a value to a member variable using the set method s.setName("lisa"); s.setAge(20); s.show(); //Get the value of a member variable using the get method System.out.println(s.getName() + "---" + s.getAge()); System.out.println(s.getName() + "," + s.getAge()); } }
- this keyword
-
this decorated variable is used to refer to the member variable. Its main function is to distinguish the local variable and the duplicate name of the member variable
-
If the parameter of the method has the same name as the member variable, the variable without this modifier refers to the parameter, not the member variable
-
The 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); } }
- this memory principle
- This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.
- Packaging idea
-
Encapsulation overview
Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
It is the simulation of object-oriented programming language to the objective world. In the objective world, the member variables are hidden inside the object, and the outside cannot 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 method provided by the class is used to operate and access the hidden information
The member variable private provides the corresponding getXxx()/setXxx() method -
Packaging benefits
Methods are used to control the operation of member variables, which improves the security of code
Encapsulate the code with methods to improve the reusability of the code
4. Construction method
- Overview of construction method
Construction method is a special method
-
Role: create the object Student stu = **new Student();
-
Format:
public class name {
Modifier class name (parameter) {}
} -
Function: mainly to complete 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(); } }
- Notes on construction method
-
Construction method creation
If no construction method is defined, the system will give a default parameterless construction method
If a constructor is defined, the default constructor is no longer available -
Overload of construction method
If you define the construction method with parameters and use the construction method without parameters, you must write another construction method without parameters -
Recommended usage
No matter whether it is used or not, the method of nonparametric construction is written manually -
Important features!
You can use band parameter construction to initialize member variables -
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("lisa"); s2.show(); //public Student(int age) Student s3 = new Student(20); s3.show(); //public Student(String name,int age) Student s4 = new Student("jennie",20); s4.show(); } }
- Standard production
-
Demand:
Define the standard student class. It is required to use the empty parameter and the parameter construction method to create objects. The objects created by the empty parameter are assigned through setXxx, and the objects created by the parameter are assigned directly, and the data is displayed through the show method -
Example code:
class Student { //Member variables private String name; private int age; //Construction method public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } //Member method p ublic 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); } } /* Two ways to create an object and assign values to its member variables 1:Use setXxx() to assign value after the object is created by nonparametric construction method 2:Using the construction method with parameters to directly create objects with attribute values */ public class StudentDemo { public static void main(String[] args) { //Use setXxx() to assign value after the object is created by nonparametric construction method Student s1 = new Student(); s1.setName("lisa"); s1.setAge(20); s1.show(); //Using the construction method with parameters to directly create objects with attribute values Student s2 = new Student("lisa",20); s2.show(); } }