What is this?
This is an object of its own, representing the object itself, and can be understood as: a pointer to the object itself.
Usage is as follows:
Use "this. member variable name" to distinguish it from local variables with the same name;
Use "this. member method name" to access member methods.
class Person{ private String name;//成员变量 private int age; Person(){} Person(String name){//局部变量 this.name=name;//1.用"this.成员变量名称"和重名的局部变量区分开来 } Person(String name,int age){ this(name); this.age=age; } String getInfo(){//成员方法 return "姓名:" + name + "\n年龄:" + age; } void print(){ System.out.println(this.getInfo());//2.用"this.成员方法名"访问成员方法。 System.out.println(getInfo());//这种情况this关键字一般不写,让编译器自动添加。 } } public class Test0505{ public static void main(String[] args){ Person p=new Person("张三",33); p.print(); } }
This() access constructor must be placed in the first line of the constructor
class Person{ private String name; private int age; Person(){} Person(String name){//不含this()的构造方法 this.name=name; } Person(String name,int age){//在构造方法内调用另一个构造方法 this(name);//3."this();"访问构造方法必须放在构造方法的第一行 this.age=age; } String getInfo(){ return "姓名:" + name + "\n年龄:" + age; } void print(){ System.out.println(this.getInfo()); } } public class Test0505{ public static void main(String[] args){ Person p=new Person("张三",33); p.print(); } }
Return to the current Reference to the object
class Leaf{ private int i=0; Leaf increment(){ i++; return this;//4.返回对当前对象的引用。 } void print(){ System.out.println("i="+i); } } public class Test0505{ public static void main(String[] args){ Leaf x=new Leaf(); x.increment().increment().increment().print(); } }
Pass a reference to the current object as a parameter to other methods
class Person{ void eat(Apple apple){ Apple peeled=apple.getPeeled(); System.out.println(peeled); } } class Apple{ Apple getPeeled(){ System.out.println(this);//输出对当前对象的引用。 return Peeler.peel(this);//5.将对当前对象的引用作为参数传递给其他方法。 } } class Peeler{ static Apple peel(Apple apple){ return apple; } } public class Test0505{ public static void main(String[] args){ Apple a=new Apple(); System.out.println(a); new Person().eat(a); } }
Recommended tutorial: Java Tutorial
The above is the detailed content of What does the this keyword in java mean?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

Atom editor mac version download
The most popular open source editor