1, Use of objects
1. Usage format of object
- Create object:
Class name object name = new Class name();
- Use objects to access members in a class:
Object name.Member variables; Object name.Member method();
- Use format of object:
Create student class
public class Student { //Member variable String name;//full name int age;//Age //Member method //Learning methods publicvoid study() { System.out.println("study hard and make progress every day"); } //How to eat publicvoid eat() { System.out.println("Learn to eat when you are hungry"); } }
give an example
public class Test01_Student { public static void main(String[] args) { //Create object format: class name object name = new class name (); Student s = new Student(); System.out.println("s:"+s); //cn.itcast.Student@100363 //Direct output of member variable values System.out.println("full name:"+s.name); //null System.out.println("Age:"+s.age); //0 System.out.println("‐‐‐‐‐‐‐‐‐‐"); //Assign values to member variables s.name = "Zhao Liying"; s.age = 18; //Output the value of the member variable again System.out.println("full name:"+s.name); //Zhao Liying System.out.println("Age:"+s.age); //18 System.out.println("‐‐‐‐‐‐‐‐‐‐"); //Call member method s.study(); // "Study hard and make progress every day" s.eat(); // "Learn to eat when you're hungry" } }
2. Default value of member variable
data type | Default value |
---|---|
Integer (byte, short, int, long) | 0 |
float, double | 0.0 |
Character (char) | '\u0000' |
boolean | false |
Array, class, interface | null |
3. Object memory diagram
-
An object that calls a method memory graph
The method of running in stack memory follows the principle of "first in first out, last in first out". The variable p points to the space in the heap memory, looks for the method information, and executes the method.
-
Memory map of two objects calling the same method
When an object calls a method, find the method information in the class according to the method tag (address value) in the object. In this way, even if there are multiple objects, only one copy of method information is saved to save memory space.
-
A reference passed as a parameter to the memory map in the method
The reference type is used as a parameter, and the address value is passed.
2, Differences between member variables and local variables
- Different positions in the class focus
Member variable: in class, outside method
Local variable: in method or on method declaration (formal parameter) - The scope of action is different and the focus is different
Member variables: in classes
Local variables: in method - Different focus of initialization value
Member variables: have default values
Local variable: no default value. Must first define, assign, and finally use - Different locations in memory
Member variables: heap memory
Local variables: stack memory - Different life cycles
Member variable: exists with the creation of the object and disappears with the disappearance of the object
Local variable: exists with the method call and disappears with the method call
3, Encapsulation
1. General
- Object oriented programming language is a simulation of the objective world. In the objective world, member variables are hidden in the object, and the outside world cannot operate and modify them directly.
- Encapsulation can be considered as a protective barrier to prevent the code and data of this class from being freely accessed by other classes. To access the data of this class, you must use the specified method. Proper encapsulation can make the code easier to understand and maintain, and also strengthen the security of the code.
- The principle is to hide attributes. If you need to access a certain attribute, provide public methods to access it.
2. Steps of packaging
- Use the private keyword to decorate member variables.
- For the member variables to be accessed, provide a corresponding pair of getXxx methods and setXxx methods.
3. Encapsulated operation - private keyword
① Meaning of private
- private is a permission modifier that represents the minimum permission.
- You can modify member variables and member methods.
- Member variables and member methods modified by private can only be accessed in this class.
② Use format of private
private Data type variable name;
- Modify the member variable with private, and the code is as follows:
public class Student { private String name; private int age; }
- Provide getXxx method / setXxx method to access member variables. The code is as follows:
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; } }
4. Package optimization 1 - this keyword
① The meaning of this
- this represents the reference (address value) of the current object of the class, that is, the reference of the object itself.
- The method in this object is called. That is, this represents who is calling.
② this use format
this.Member variable name;
- Use the variable in this modification method to solve the problem that the member variable is hidden. The code is as follows:
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; } }
5. Package optimization 2 - construction method
- When an object is created, the constructor is used to initialize the object and assign the initial value to the member variable of the object.
- Whether you customize the construction method or not, all classes have construction methods, because Java automatically provides a parameterless construction method. Once you define the construction method, the default parameterless construction method automatically provided by Java will become invalid.
① Definition format of construction method
Modifier constructor name(parameter list){ // Method body }
The method name is the same as the class name of the constructor. It doesn't have a return value, so it doesn't need a return value type, or even void. After using the construction method, the code is as follows:
public class Student { private String name; private int age; // Nonparametric construction method public Student() {} // Parametric construction method public Student(String name,int age) { this.name = name; this.age = age; } }
② Precautions
- If you do not provide a construction method, the system will give a parameterless construction method.
- If you provide a construction method, the system will no longer provide a parameterless construction method.
- The construction method can be overloaded, and parameters can be defined or not.
4, Standard code -- JavaBean
- JavaBean is a standard specification of classes written in Java language. Classes that conform to JavaBean are required to be concrete and public, and have parameterless construction methods, providing set and get methods for operating member variables.
public class ClassName{ //Member variable //Construction method //Nonparametric construction method [required] //Parametric construction method [suggestion] //Member method //getXxx() //setXxx() }
- Write classes that conform to JavaBean specifications. Take student classes as an example. The standard code is as follows:
public 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; } //get,set method publicvoid setName(String name) { this.name = name; } public String getName() { return name; } publicvoid setAge(int age) { this.age = age; } publicint getAge() { return age; } }
- Test class, code as follows:
public class TestStudent { public static void main(String[] args) { //Use of parameterless construction Student s= new Student(); s.setName("Liuyan"); s.setAge(18); System.out.println(s.getName()+"‐‐‐"+s.getAge()); //Use of structure with parameters Student s2= new Student("Zhao Liying",18); System.out.println(s2.getName()+"‐‐‐"+s2.getAge()); } }