When introducing how to use the instanceof keyword to develop more conveniently in Android, let’s first review the concept of instanceof in Java.
Most of the concepts of instanceof are defined as follows: instanceof is a binary operator in Java, and ==, >, and < are the same type of things. Since it is composed of letters, it is also a reserved keyword in Java. Its function is to test whether the object on its left is an instance of the class on its right and return data of type boolean. Take 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 the object it points to is an instance of the Object class. Obviously, this is true, so we return true, that is, the value of isObject is True.
instanceof has some uses. For example, we wrote a system for handling bills, which has three categories:
public class Bill {//Omit details} public class PhoneBill extends Bill {//Omit details} public class GasBill extends Bill {//Omit details}There is a method in the handler to accept an object of type Bill and calculate the amount. Assume that the two bill calculation methods are different, and the incoming Bill object may be either of the two, so use instanceof to judge:
public double calculate(Bill bill) { if (bill instanceof PhoneBill) { //calculate the phone bill} if (bill instanceof GasBill) { //calculate the gas bill} ... }This way, two subclasses can be processed in one method.
However, this approach is often considered as a failure to take advantage of object-oriented polymorphisms. In fact, the above functions require that method overloading can be achieved completely. This is the method of object-oriented becoming the right one to avoid returning to the structured programming mode. Just provide two names and return values and accept methods with different parameter types:
public double calculate(PhoneBill bill) { //calculate the phone bill} public double calculate(GasBill bill) { //calculate the gas bill} Therefore, using instanceof is not a recommended approach in most cases, and polymorphism should be used well.
I copied the above. I thought it was written well and I had a clear introduction, so I took it to quote it. It can be seen that the key to instanceof is to determine whether the object on the left is an instance of the class on the right. If so, it can handle the next logic.
In Android, the instanceof keyword is often used to call the Activity method in a Fragment. For example, you need to call a method in the current Activity in the Fragment. Some people say, then I will just rewrite one, right? What if there is a special method, fragment does not support it? At this time, instanceof was used, for example:
if( getActivity() instance of IndexActivity) ((IndexActivity) getActivity()).showccaidan();
As can be seen from the above two lines of code, if the current Fragment is a fragment of IndexActivity, then after calling the method in the activity for a long time, you must force the current Activity before calling it.
In fact, not only can the instanceof keyword be used in Fragment, but also in custom Adapters:
if(context instance of CommodityWarningActivity){ holder.entName_ll.setVisability(View.VISIBLE); holder.entName.setText(list.get(arg0).getStrCorporationName()); }else{ holder.entName_ll.setVisability(View.GONE); }context should be known as Android. Context literally means context, or scene, which is a process of operation between users and operating systems. (Isn't it that I know the context, you can go and check the information first). So if you need a context, you can use the instanceof keyword.
The above is a detailed explanation of the usage examples of the instanceof keyword in Java in Android introduced to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!