zoukankan      html  css  js  c++  java
  • Java-instanceof关键字

    一、instanceof

      在编写代码过程中,如果不知道一个对象属于哪一个类,这时instanceof关键字起到决定性作用,他会以boolean(truefalse)反馈结果

    class A{
        public void fun1(){
            System.out.println("A----->public void fun1");
        }
        public void fun2(){
            this.fun1();
        }
    }
    class B extends A{
        public void fun1(){  //覆写父类fun1方法
            System.out.println("B-->public void fun1(B)");
        }
        public void fun3(){
            System.out.println("B---->public void fun3(B)");
        }
    }
    class C extends A{
        public void fun1(){ //覆写父类fun1方法
            System.out.println("C---->public void fun1(C)");
        }
        public void fun5(){
            System.out.println("C------>public void fun5(C)");
        }
    }
    public class DomeA {
        public static void main(String[] args){
    
            A a1 = new B(); //通过向上转型实例化A类对象
            System.out.println("A a1 = new B():"+(a1 instanceof A));
            System.out.println("A a1 = new B():"+(a1 instanceof B));
    
            A a2 = new A();  //通过A类的构造实例化本类对象
            System.out.println("A a2 = new A():"+(a2 instanceof A));
            System.out.println("A a2 = new A():"+(a2 instanceof B));
        }
    }
    //A a1 = new B():true
    //A a1 = new B():true
    //A a2 = new A():true
    //A a2 = new A():false
  • 相关阅读:
    1295: [SCOI2009]最长距离
    [vijos p1028] 魔族密码
    HJ浇花
    1060: [ZJOI2007]时态同步
    1816: [Cqoi2010]扑克牌
    1800: [Ahoi2009]fly 飞行棋
    4300: 绝世好题
    1237: [SCOI2008]配对
    1801: [Ahoi2009]chess 中国象棋
    1189: [HNOI2007]紧急疏散evacuate
  • 原文地址:https://www.cnblogs.com/lcj0703/p/5818037.html
Copyright © 2011-2022 走看看