In this chapter we will discuss some of this
1 This is used inside a class, and it points to a reference to an object.
Package Com.ray.ch01;public class Test {private Test gettest () {return this;} public static void Main (string[] args) {}}
The this in the above code is the object that returns the type test.
This can also be done by:
Package Com.ray.ch01;public class Test {private void Say () {System.out.println ("method say");} private void Testsay () {This.say ();} public static void Main (string[] args) {new Test (). Testsay ();}}
Output:
Method say
The above code operates say this method by referencing the object of test.
In fact, it doesn't matter if you don't use this inside the class, it just adds readability, and the following code shows us this:
Package Com.ray.ch01;public class Test {private void Say () {System.out.println ("method say");} private void TestSay1 () {This.say ();} private void TestSay2 () {Say ();} public static void Main (string[] args) {new test (). TestSay1 (); new test (). TestSay2 ();}}
Output:
Method say
Method say
From the output can be seen, in fact there is no this, all can call the internal method.
2 This can better distinguish between parameters and domains.
When we encode, we use many parameters with the same name as the domain, so using this allows us to better differentiate between the two.
For example:
Package Com.ray.ch01;public class Test {private int id;public test (int id) {id = ID;} public static void Main (string[] args) {}}
The code above is very difficult to tell how the two IDs are assigned, so let's add this here to get a better understanding of the code.
Package Com.ray.ch01;public class Test {private int id;public test (int id) {this.id = ID;} public static void Main (string[] args) {}}
3 This can refer to the previous constructor.
Package Com.ray.ch01;public class Test {private int id;private String name;public Test () {System.out.println ("created Tes T ");} public Test (int id) {this (); this.id = ID; SYSTEM.OUT.PRINTLN ("ID:" + ID);} public Test (int ID, String name) {this (id); this.name = name; System.out.println ("Name:" + name);} public static void Main (string[] args) {new Test (1, "Jack");}}
Output:
Created Test
Id:1
Name:jack
Because this is used, it makes the code more indirect.
Attention:
(1) The constructor referenced by this () must be placed in the first sentence, or it will be an error.
(2) The constructor referenced by this () cannot use more than two at a time.
(3) This can only refer to the normal method, and this () can only refer to the constructor method.
4 This cannot refer to the static method
Package Com.ray.ch01;public class Test {private static void Say () {System.out.println ("method say");} public static void Main (string[] args) {Test.say ();}}
A static method is a class method that does not need to create an object to use, but this method cannot be referenced by this.
Summary: In this chapter we mainly discuss the four points of attention and the implementation of the note in the code.
2.this
2.1 This is used internally within a class, and it points to a reference to an object.
Package Com.ray.ch01;public class Test {private Test gettest () {return this;} public static void Main (string[] args) {}}
The this in the above code is the object that returns the type test.
This can also be done by:
Package Com.ray.ch01;public class Test {private void Say () {System.out.println ("method say");} private void Testsay () {This.say ();} public static void Main (string[] args) {new Test (). Testsay ();}}
Output:
Method say
The above code operates say this method by referencing the object of test.
In fact, it doesn't matter if you don't use this inside the class, it just adds readability, and the following code shows us this:
Package Com.ray.ch01;public class Test {private void Say () {System.out.println ("method say");} private void TestSay1 () {This.say ();} private void TestSay2 () {Say ();} public static void Main (string[] args) {new test (). TestSay1 (); new test (). TestSay2 ();}}
Output:
Method say
Method say
From the output can be seen, in fact there is no this, all can call the internal method.
2.2 This can better distinguish between parameters and domains.
When we encode, we use many parameters with the same name as the domain, so using this allows us to better differentiate between the two.
For example:
Package Com.ray.ch01;public class Test {private int id;public test (int id) {id = ID;} public static void Main (string[] args) {}}
The code above is very difficult to tell how the two IDs are assigned, so let's add this here to get a better understanding of the code.
Package Com.ray.ch01;public class Test {private int id;public test (int id) {this.id = ID;} public static void Main (string[] args) {}}
2.3 This can refer to the previous constructor.
Package Com.ray.ch01;public class Test {private int id;private String name;public Test () {System.out.println ("created Tes T ");} public Test (int id) {this (); this.id = ID; SYSTEM.OUT.PRINTLN ("ID:" + ID);} public Test (int ID, String name) {this (id); this.name = name; System.out.println ("Name:" + name);} public static void Main (string[] args) {new Test (1, "Jack");}}
Output:
Created Test
Id:1
Name:jack
Because this is used, it makes the code more indirect.
Attention:
(1) The constructor referenced by this () must be placed in the first sentence, or it will be an error.
(2) The constructor referenced by this () cannot use more than two at a time.
(3) This can only refer to the normal method, and this () can only refer to the constructor method.
2.4 This cannot refer to the static method
Package Com.ray.ch01;public class Test {private static void Say () {System.out.println ("method say");} public static void Main (string[] args) {Test.say ();}}
A static method is a class method that does not need to create an object to use, but this method cannot be referenced by this.
Summary: This chapter mainly discusses the four methods of this and the implementation in the code.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Know java-4.4 this from the beginning