On the fifth day, I learned how to use the private keyword. Today, I will first make a simple use of it
Let's first define a student class
public 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); } }
Write another test class and take it as the main class
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 get to get the value of the member variable System.out.println(s.getName() + "----" + s.getAge()); } }
Next, learn this keyword, continue to use the above example, and change the student class to
public class Student { //Member variable private String name; private int age; //get/set method //public void setName(String n) { // name = n; //} public void setName(String name) { //The name modified with this is a member variable, and the local variable is not modified this.name = name; } public String getName() { return name; } //public void setAge(int a) { // age = a; //} public void setAge(int age){ this.age=age; } public int getAge() { return age; } public void show() { System.out.println(name + "," + age); } }
The test class remains unchanged. It is found that if the name of the local variable in the setName method is also set to the same, the name or age will become null when outputting
This is because the program cannot automatically identify local variables and member variables. At this time, the function of this keyword is proposed
① The variable modified by this refers to the member variable
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 value of the variable without this modification is the member variable
② When to use this? Resolve hidden member variables for local variables
③ this: represents the object reference of the class
this represents the object to which the method is called
After learning the basic use of this, you need to understand the memory principle of this
After learning keywords, I finally entered the learning of encapsulation. java is an object-oriented language. It has three characteristics: encapsulation, inheritance and polymorphism.
Principle of encapsulation: some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation of hidden information and access to member variable private are realized through the methods provided by the class, and the corresponding getXxx()/setXxx() methods are provided
Advantages of encapsulation: the operation of member variables is controlled by methods, which improves the security of the code.
The code is encapsulated by method, which improves the reusability of the code.
In the above code exercise, I have actually learned the use of encapsulation. Now I want to learn a special method
Construction method: it is used to create objects. Its main function is to complete the initialization of object data.
Format:
public class class name{
Modifier class name (parameter){
}
}
Let's take a look at an example to create a student class
public class Student { //Member variable private String name; private int age; //Construction method public Student() { System.out.println("non-parameter constructor "); } public void show() { System.out.println(name + "," + age); } }
Write another student test class
public class StudentDemo { public static void main(String[] args) { //create object Student s = new Student(); s.show(); } } /*The final output is non-parameter constructor null,0 */
You can see that the constructor is not called in the test class, but appears in the result. We called the constructor when we called Student().
In the calling method without a constructor, the system will call a parameterless constructor by default, and naturally there will be no output. However, if a constructor is used, the system will no longer provide a default parameterless constructor, which needs to be given manually
Create a new student class
public class Student { //Member variable private String name; private int age; // public Student() { // System.out.println("parameterless constructor"); // } //Manually given parameterless constructor 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 s = new Student(); s.show(); Student s2 = new Student("Lin Qingxia"); s2.show(); Student s3 = new Student(30); s3.show(); Student s4 = new Student("Lin Qingxia",30); s4.show(); } } /* The final output is null,0 Lin Qingxia, 0 null,30 Lin Qingxia, 30 */
At the end of today, let's learn about the production of standard classes
① Member variable
Use private decoration
② Construction method
Provides a parameterless construction method
Provides a construction method with multiple parameters
③ Member method
Provide getXxx()/setXxx() corresponding to each member variable
Provides a show() that displays object information
④ There are two ways to assign values to the member variables of the wear object
After the object is created by the parameterless constructor, setXxx() is used for assignment
Use the construction method with parameters to directly create objects with attribute values