To learn more about how to use instanceof keyword development in Android, let's review the concept of instanceof in Java.
Instanceof Most of the concepts are defined in this way: Instanceof is a two-dollar operator of Java, and ==,>,< is the same kind of thing. Because it is made up of letters, it is also a reserved keyword in java. Its role is to test whether the object on its left is an instance of its right class, and returns a Boolean type of data. Give me a chestnut:
String s = "I AM an object!";
Boolean IsObject = s instanceof Object;
We declare a String object reference, point to a String object, and then use INSTANCOF to test whether it points to an instance of the object class, which, obviously, is true, so return true, that is, the IsObject value is true.
Instanceof is of some use. For example, we wrote a billing system with three classes:
public class Bill {//omit details} public
class Phonebill extends Bill {//omit details} public
class Gasbill extends Bill {//Province Slightly details}
There is a method in the handler that accepts a bill type object and calculates the amount. Suppose the two bills are calculated differently, and the incoming Bill object may be either of two, so use instanceof to determine:
Public double Calculate (Bill Bill) {
if (Bill instanceof Phonebill) {
//Calculate phone bill
}
if (Bill instance of Gasbill) {
//Calculate gas bill
}
...
}
This allows you to handle two of seed classes in one way.
However, this approach is often considered to be a failure to take advantage of the object-oriented polymorphism. In fact, the above features require method overload can be fully implemented, this is an object-oriented approach should be to avoid the return to structured programming mode. As long as two names and return values are the same, methods that accept different parameter types are available:
Public double Calculate (Phonebill Bill) {/
/Calculate phone bill
} public
double calculate (Gasbill Bill) {
/ /Calculate gas Bill
}
Therefore, the use of instanceof in most cases is not recommended practice, should make good use of polymorphism.
I copied the above, think that the writing is not bad, the introduction is clear, take over to quote. It can be seen that the key to the instanceof is to determine whether the left side of the object is an instance of the right class, if it is, you can handle the next logical.
In Android, the instanceof keyword is often used to invoke the activity method in fragment. For example, you need to call a method in the current activity in the fragment, and some people say, I'm just going to write it back. If there is a special way, fragment not support it? At this time, instanceof was used and a chestnut was raised:
if (getactivity () instanceof indexactivity)
As you can see from the above two lines of code, if the current fragment is a indexactivity fragment, then call the method in the activity so long that the current activity must be strongly forwarded before the call.
In fact, not only fragment can be used in the INSTANCEOF keyword, custom adapter can also be used:
If (context instanceof commoditywarningactivity) {
holder.entName_ll.setVisibility (view.visible);
Holder.entName.setText (List.get (arg0). Getstrcorporationname ());
else{
holder.entName_ll.setVisibility (view.gone);
}
The context of this Android should know that contexts are literally contextual, or called scenes, a process in which users and operating systems operate. (It is not special to know the context to see the information first). So you can use the INSTANCEOF keyword if you have a context where you need it.
The above is a small set of Java in the introduction of the INSTANCEOF keyword in the use of Android in the example detailed, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!