• isAssignableFrom


    instanceof 针对实例 
    isAssignableFrom针对class对象

     

    isAssignableFrom   是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口。   
    通常调用格式是  

    Java代码  收藏代码
    1. Class1.isAssignableFrom(Class2)     

     

    调用者和参数都是   Java.lang.Class   类型。   
          
      而   instanceof   是用来判断一个对象实例是否是一个类或接口的或其子类子接口的实例。   
        格式是:   

    Java代码  收藏代码
    1. oo   instanceof   TypeName       

         第一个参数是对象实例名,第二个参数是具体的类名或接口名,例如   String,InputStream。

     

    Java代码  收藏代码
    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         List<String> list = new ArrayList<String>();  
    4.         System.out.println(list instanceof List);  
    5.         System.out.println(list instanceof ArrayList);  
    6.           
    7. /*      System.out.println(list.getClass()); 
    8.         System.out.println(List.class); 
    9.         System.out.println(ArrayList.class);*/  
    10.           
    11.         System.out.println(list.getClass().isAssignableFrom(List.class));  
    12.         System.out.println(List.class.isAssignableFrom(list.getClass()));  
    13.     }  
    14. }  

     

    结果:

    true
    true
    false
    true

    其中instanceof是子-->父 
    isAssignableFrom是父-->子

     

     

    我们应该尽量少用instanceof 运算符

    应该尽量用多态来替代它

     

    Java代码  收藏代码
    1. public interface Accounts {     
    2.   
    3. }  
    4.   
    5. public class WaterAccounts implements Accounts {  
    6.       
    7. }  
    8.   
    9. public class ElectricityAccounts implements Accounts {   
    10.       
    11. }  

     

    Java代码  收藏代码
      1. //客户端    
      2. public class test {  
      3.   
      4.     public static void main(String[] args) {  
      5.   
      6.         Accounts accsWater = new WaterAccounts();  
      7.         Accounts accsElectricity = new ElectricityAccounts();  
      8.   
      9.         acceptAcounts(accsWater);  
      10.   
      11.     }  
      12.   
      13.     // 第一种使用instanceof  
      14.     public static void acceptAcounts(Accounts accs) {  
      15.   
      16.         if (accs instanceof WaterAccounts) {  
      17.   
      18.             System.out.println("收水费");  
      19.   
      20.         } else if (accs instanceof ElectricityAccounts) {  
      21.   
      22.             System.out.println("收电费");  
      23.         }  
      24.   
      25.     }  
      26.   
      27.     // 第二种使用多态  
      28.     public static void acceptAcounts(WaterAccounts accs) {  
      29.   
      30.         System.out.println("收水费");  
      31.   
      32.     }  
      33.   
      34.     public static void acceptAcounts(ElectricityAccounts accs) {  
      35.   
      36.         System.out.println("收电费");  
      37.   
      38.     }  
      39.   

  • 相关阅读:
    The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make s
    ScrollView 定位
    Fragment获取Activity,Activity获取Fragment
    Popupwindow全屏问题
    bzoj千题计划310:bzoj5285: [Hnoi2018]寻宝游戏(思维题+哈希)
    bzoj千题计划309:bzoj4332: JSOI2012 分零食(分治+FFT)
    2016vijos 1-3 兔子的晚会(生成函数+倍增FWT)
    bzoj千题计划308:bzoj4589: Hard Nim(倍增FWT+生成函数)
    bzoj千题计划307:bzoj5248: [2018多省省队联测]一双木棋
    cdqz2017-test10-加帕里图书馆(区间DP & 简单容斥)
  • 原文地址:https://www.cnblogs.com/1995hxt/p/5724073.html
Copyright © 2020-2023  润新知