This article mainly introduces the relevant information on how to use the Java this keyword in detail. I hope this article can help everyone and let everyone fully understand this part of the content. Friends in need can refer to it
Detailed explanation of the use of Java this keyword
This keyword in the constructor
The constructor is an object of a class that passes the new key It is automatically called when the word is created. It cannot be called through the method name (that is, the class name) like other methods in the program. But if a class has multiple constructors, you can call other constructors through this (paras...) in one constructor.
Using this to call other constructors has the following constraints.
1) Other constructors can only be called through this in the constructor, and cannot be used in ordinary methods.
2) The constructor cannot be called recursively through this, that is, the constructor itself cannot be called directly or indirectly through this in a constructor.
For example:
class test { test() { this(1); } test(int a){ this(); } test(int a, int b) { this(1, 2); } }
The test(int) constructor is called in the test() method, and the test(int) constructor is called The test() constructor is added to form a recursive call. Calling itself in test(int, int) also constitutes a recursive call. All are not allowed.
3) Calling other constructors through this must be executed in the first line of the constructor. Since super calls the constructor of the parent class must also be executed in the first line of the constructor, therefore, calling the constructor through this and super cannot appear in the same constructor at the same time. You cannot call different constructors multiple times in one constructor.
You can also use the this keyword in the constructor to access member variables and member functions in this class. Its usage is the same as the this keyword in non-constructor methods.
This keyword in non-constructor methods
In Java, you can call member variables and methods in a class through the this keyword. Its usage is.
1) this.xxx; access the member variable xxx in the class
2) this.yyy(paras…); access the member method yyy in the class
3) this; currently References to class objects
The this keyword is not subject to access permission control when accessing member variables and member functions of the class. It can access all member variables and methods in this class, including private member variables and method. You can also access static members of this class through this. However, since static members can be directly accessed through the class name, if you access through this, there will be a warning message "The static field ××× should be accessed in a static way". This cannot be used within a static member of a class or within a static block.
This keyword under inheritance relationship
Under inheritance relationship, the this keyword in the parent class does not always represent the variables and methods in the parent class. The four uses of this keyword are as mentioned above and are listed below.
1) this(paras…); Access other constructors
2) this.xxx; Access member variables xxx in the class
3) this.yyy(paras…) ; Access member methods in the classyyy
4) this; Reference to the current class object
For the first type, regardless of whether the subclass has a constructor with the same parameters, this(paras...) ;The access is always to the constructor in the parent class.
For the second type, regardless of whether the subclass covers the member variable, this.xxx; always accesses the member variable in the parent class.
For the third type, if the subclass overrides the member method, then this.yyy(paras...); accesses the member method of the subclass. If the subclass does not override the member method, then this.yyy (paras...); accesses the member methods of the parent class.
For the fourth type, this always represents the object of the subclass.
For example:
public class ClassTest { public static void main(String[] args) { Child child = new Child(); child.show(); } } class Parent { public String str; Parent(){ this(1); } Parent(int a) { this.str = "Parent"; this.show(); } public void show() { System.out.println(this.str); } } class Child extends Parent { public String str; Child() { } Child(int a) { str = "Child"; } public void show() { System.out.println(str); super.show(); } }
There are two statements in the main() function, new Child() and child.show().
When the first statement new Child() is executed, the constructor of the Child class will be executed. However, the Child class is a subclass of the Parent class, so the constructor of the Parent class will be executed first. The parameterless constructor of the Child class does not use super and this to call the parent class or other constructors in this class, so the parameterless constructor of the parent class will be called. This(1) is called and executed in the parameterless constructor Parent() of the parent class. This call means executing the constructor with an integer parameter in the parent class. Although there is also a constructor with an integer parameter in the subclass, But it will not be executed. The constructor method with an integer parameter in the parent class executes this.str="Parent". This.str here represents the member variable str in the parent class. Although there is also a member variable str in the subclass, it will not be used. Assignment. After assigning the member variable str in the parent class to "Parent", then this.show() is executed. Although there is a show() method in the parent class, because the subclass overrides the show() method, this. The show() method of the subclass executed by show(). The show() method of the subclass first performs the operation of printing str. At this time, what is printed is obviously the str in the subclass. The str of the subclass is not assigned a value because null is printed. Then the show() method of the subclass executes super.show(), that is, the show() method of the parent class is called. In the show() method of the parent class, the operation of printing this.str is performed. This.str also represents The member variable str in the parent class, so "Parent" is printed.
第二条语句child.show()先是执行子类的show()方法,子类的show()先是打印了子类的str值(null),然后执行了父类的show()打印了父类的str值(”Parent”)。
两条语句的打印结果为null, Parent, null, Parent。
如果将第一条语句改为new Child(1),则执行的是子类的有一个整数参数的构造方法,仍然是先执行父类的无参构造方法,初始化父类的str为”Parent”,然后执行子类的show(),子类的show()打印子类的str值(null),然后执行父类的show(),父类show()打印父类的str值(”Parent”),然后执行子类的构造方法将子类的str初始化为”Child”。 第二条语句child.show()先是执行子类的show()方法,子类的show()先是打印了子类的str值(”Child”),然后执行了父类的show()打印了父类的str值(”Parent”)。
两条语句的打印结果为null, Parent, Child, Parent。
super和this的异同
super在一个类中用来引用其父类的成员,它是在子类中访问父类成员的一个桥梁,并不是任何一个对象的引用,而this则表示当前类对象的引用。在代码中Object o = super;是错误的,Object o = this;则是允许的。
super关键字的作用在于当子类中覆盖了父类的某个成员变量,或者重写了父类的某个成员方法时还能够访问到父类的成员变量和成员方法。如果子类中没有重写父类的成员变量和成员方法,则子类会继承父类的所有非private的成员变量和成员方法。这时在子类中无论通过this来访问成员和通过super来访问成员,结果都是一样的。
super.getClass()和this.getClass()
getClass()是Object类定义的一个final方法,所有Java类的getClass()都继承自Object类。如前文所述,如果子类没有重写父类的某个成员方法,那么通过super来访问还是还是通过this来访问结果都是一样的。因此,super.getClass()和this.getClass()结果是一样的。Object类的getClass()方法返回的是该对象的运行时类,一个对象的运行时类是该对象通过new创建时指定的类。因此,super.getClass()和this.getClass()返回的都是new对象时指定的类。
例如:
public class ClassConstructorTest { public static void main(String[] args) { Child child = new Child(); child.show(); } } class Parent { private Parent mSelf; Parent(){ mSelf = this; } public void show() { System.out.println(this.getClass().getName()); System.out.println(super.getClass().getName()); System.out.println(mSelf.getClass().getName()); } } class Child extends Parent { public void show() { System.out.println(this.getClass().getName()); System.out.println(super.getClass().getName()); super.show(); } }
打印的类名都是Child。
The above is the detailed content of Detailed explanation of the use of this keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
