[appendix 2Java object-oriented programming] detailed explanation of inventory keywords this, super and final

❀ Write in front
❀ Blog home page: Hard working Naruto
❀ Series column: Java basic learning πŸ˜‹
❀ Welcome, friends, praise πŸ‘ follow πŸ”Ž Collection πŸ” Learning together!
❀ If there are mistakes, please correct them! 🌹

πŸ”₯ Series portal:
[chapter 04 Java object-oriented programming (Part 2)] the killer technology of object-oriented programming
[chapter 04 Java object-oriented programming (middle)] decrypt inheritance and polymorphism
[chapter 04 Java object-oriented programming (Part I)] the first experience of everything being an object [ second in the hot list of the whole station ]
[Appendix 1 common algorithms in Java arrays] the ten sorting algorithms are illustrated and explained in detail, giving you endless aftertaste
[chapter 03 Java arrays] programmers must see the detailed explanation of arrays
[chapter 02 basic Java syntax] the detailed explanation enables you to re understand the basic Java syntax and process control
[chapter 01 overview of Java language] Java has been studied for a long time. Come back and get familiar with it (detailed)

1, this

  1. Introduction: in Java
    ● it is used inside the method to represent the reference of the object to which the method belongs
    ● it is used inside the constructor to represent the object that the constructor is initializing
  2. this can call the properties, methods, and constructors of a class
  3. When to use: use this when the object calling the method needs to be used in the method
    Specifically: we can use this to distinguish between attributes and local variables: for example, this name = nameοΌ›
  4. Use this to call properties and methods:
class Person{ // Define the Person class
		private String name ;
		private int age ;
	public Person(String name,int age){
		this.name = name ;
		this.age = age ; }
	public void getInfo(){
		System.out.println("full name:" + name) ;
		this.speak();
	}
	public void speak(){
		System.out.println("Age: + this.age);
	}
}
  1. Use this to call the current object:
class Person{ // Define the Person class
		String name;
	Person(String name){
		this.name = name;
		}
	public void getInfo(){
		System.out.println("Person class --> " + this.name);
		}
	public boolean compare(Person p){
		return this.name==p.name;
	}
}
	public class PersonTest{
	
	public static void main(String args[]){
		Person per1 = new Person("Zhang San") ;
		Person per2 = new Person("Li Si") ;
		per1.getInfo() ; // The object that currently calls the getInfo() method is per1.
		per2.getInfo() ; // The object that currently calls the getInfo() method is per2.
		boolean b = per1.compare(per2);
	} 
}
  1. Use this to call the constructor of this class:
class Person{ // Define the Person class
		private String name ;
		private int age ;
		public Person(){ // Parameterless constructor
		System.out.println("New object instantiation") ;
}
	public Person(String name){
		this(); // Call the parameterless constructor in this class
		this.name = name ;
}
	public Person(String name,int age){
		this(name) ; // Call constructor with one parameter
		this.age = age;
}
	public String getInfo(){
		return "full name:" + name + ",Age:" + age ;
	}
}

2, super

  1. Use super to call the specified operation in the parent class:
    ● super can be used to access the attributes defined in the parent class
    ● super can be used to call member methods defined in the parent class
    super can be used to call the parent class constructor in the subclass constructor.
  2. The usage of super is similar to this. This represents the reference of this class object, and super represents the identification of the memory space of the parent class
    Β 
    super traceability is not limited to direct parent classes
  3. Example of keyword super:
class protected Person {
		String name = "Zhang San"; 
		protected int age;
	public String getInfo() {
		return "Name: " + name + "\nage: " + age;
	}
}
class Student extends Person {
		protected String name = "Li Si";
		private String school = "New Oriental";
	public String getSchool() {
		return school;
	}
	public String getInfo() {
		return super.getInfo() + "\nschool: " + school;
	}
}
	public class StudentTest {
	public static void main(String[] args) {
		Student st = new Student();
	System.out.println(st.getInfo());
	}
}

πŸ‘Œ Call the constructor of the parent class

  1. By default, all constructors in the subclass will access the constructor of the null parameter of the parent class
  2. When there is no constructor with empty parameters in the parent class, the constructor of the child class must call the corresponding constructor in this class or the parent class through the this (parameter list) or super (parameter list) statement. At the same time, you can only "choose one from two" and must be placed in the first line of the constructor
  3. If neither the parent class nor the constructor of this class is explicitly called in the subclass constructor, and there is no parameterless constructor in the parent class, there is a compilation error
  4. Example of calling parent class constructor:
public class Person {
		private String name;
		private int age;
		private Date birthDate;
	public Person(String name, int age, Date d) {
		this.name = name;
		this.age = age;
		this.birthDate = d;
	}
	public Person(String name, int age) {
		this(name, age, null);
	}
	public Person(String name, Date d) {
		this(name, 30, d);
	}
	public Person(String name) {
		this(name, 30);
	}
}
public class Student extends Person {
		private String school;
	public Student(String name, int age, String s) {
		super(name, age);
		school = s;
	}
	public Student(String name, String s) {
		super(name);
		school = s;
	}
	// Compilation error: no super(), the system will call the parameterless constructor of the parent class.
	public Student(String s) {
		school = s;
	}
}

πŸ‘Œ The difference between this and super

3, final

The keyword final can be used to modify it to represent the final

  1. Classes marked final cannot be inherited
  2. A method of a final tag cannot be overridden by a subclass
  3. Variables (member variables or local variables) marked with final are called constants
  4. Application example of keyword final:
public final class Test {
		public static int totalNumber = 5;
		public final int ID;
	public Test() {
		ID = ++totalNumber; // The "variable" of the final modifier can be assigned a value in the constructor
	}
	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t.ID);
		final int I = 10;
		final int J;
		J = 20;
		J = 30; // illegal
	}
}

🎁 Conclusion: the flexible use of this, super and final keywords will change the amount of code, and understanding the details will also reduce the usual troublesome bug s. Take notes and look at them often

πŸ‘Œ The author is a Java beginner. If there are errors in the article, please comment and correct them in private letters and learn together~~
😊 If the article is useful to the friends, praise it πŸ‘ follow πŸ”Ž Collection πŸ” Is my biggest motivation!
🚩 Step by step, nothing to a thousand miles, book next time, welcome to see you again 🌹

Keywords: Java Back-end

Added by thirteen13 on Sat, 15 Jan 2022 02:21:44 +0200