About the use of this in Android explains the origin of the problem
Because many students do not have a deep understanding of Java when they learn Android, many people will have doubts about the various this in the code.
Take "first line of Android" P37 page, P43 page code for example:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); } });}
Some students feel very strange, this is used to refer to what? Does it mean the same as class?
Then why firstactivity Use this, and secondactivity to use class?
Three ways to use the This 1. Represents a reference to the current object
Take the following code as an example:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button play = (Button) findViewById(R.id.play); Button stop = (Button) findViewById(R.id.stop); play.setOnClickListener(this); stop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: Videoplay(); break; case R.id.stop: Videostop(); break; } }}
The This in Play.setonclicklistener (this) is a reference to the current class mainactivity.
So why would you use Firstactivity.this to refer to firstactivity in the starting code?
We carefully analyzed the upper and lower sections of the code, found that this is the class is not the same, the above code, this exists in the anonymous class View.onclicklistener ()!
The anonymous class View.onclicklistener () is the inner class of firstactivity, and if we use this in an inner class, the class he refers to should be View.onclicklistener ().
When compiling our code, the Android compiler will find: "Eh view.onclicklistener () you ah? How do I start secondactivity from you? " Then the happy error.
So in the inner class, if we need to get its outer class, we'll use the "external class. This" notation.
- Note: ". Class" is a notation for other classes.
2. Represents a member variable with a class, not a function parameter
Take the following code as an example:
public class Book { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; }}
In the code we define a book class and generate the corresponding getter and setter methods. We found this in the setter method, and it is this. instead of. this!
With a closer look at the SetID method, we find that several of the IDs in it correspond to different things.
The ID in setId (int id) refers to the parameters given when the method is called externally, such as 1 in the new book Class variable Book,book.setid (1).
ID in This.id We obviously want him to be in the book class. The ID in the member variable Id,=id should obviously be given the argument, and the SetID (int id) is the same thing.
So this is to prevent the compiler from thinking that the three IDs are the same thing, specifically indicating that the second this is the member variable ID in the book class.
- Some students may be confused with this one in the first usage, think this is not pointing to the SetID method? This means that you are confusing the difference between classes and methods. This refers to a class, not a method.
3. Constructor for referencing a specified parameter type in a constructor method
This may be a bit complicated, because this is mostly used in Java, where a method may correspond to a number of construction methods, such as:
public class Student public Student() } public Student(String name) } public Student(int id) }}
To make it easier for us to reduce the number of methods, then if we modify the above code into:
public class Student public Student() { this(“HelloWorld”); } public Student(String name) { } public Student(int id) { }}
This appears again, this time this is in the constructor, which means that when you call the student class without arguments, the compiler automatically thinks that you called the student in the student class using the "HelloWorld" argument (String Name), if changed to this (1), that is the student (int id) construction method.
-
Note: The third usage, each constructor method can only refer to a constructor method! and must be in the starting position.
public class Student public Student() { this(“HelloWorld”); } public Student(String name) { this(1); } public Student(int id) { }}
This is in accordance with the specification, whether you use student () or student ("..."), are equal to student (1);
public class Student public Student() { this(“HelloWorld”); this(1); } public Student(String name) { } public Student(int id) { }}
This is wrong when you use student () when the compiler will not know whether to use student ("HelloWorld") or student (1) and choose to die.
The use of this in Android