`

java中instanceof用法

    博客分类:
  • Java
阅读更多

public interface A{}
public class B implements A{
  public static void main(String[] args){
     A a=null;
     B b=null;
     if(a instanceof A){
       System.out.println("true 1");
     }
     else{
       System.out.println("false 1");
     }

     if(b instanceof B){
       System.out.println("true 2");
     }
     else{
       System.out.println("false 2");
     }  
     a=new B();  
     if(a instanceof A){
       System.out.println("true 3");
     }
     else{
       System.out.println("false 3");
     }

     if(a instanceof B){
       System.out.println("true 4");
     }
     else{
       System.out.println("false 4");
     }

     b=new B();  
     if(b instanceof A){
       System.out.println("true 5");
     }
     else{
       System.out.println("false 5");
     }

     if(b instanceof B){
       System.out.println("true 6");
     }
     else{
       System.out.println("false 6");
     }
  }
}
输出结果是:
false 1
false 2
true 3
true 4
true 5
true 6 

 

原文地址:http://blog.csdn.net/j2eejia/archive/2007/08/09/1733871.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics