java 零散知识汇总(初级)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 零散知识汇总(初级)相关的知识,希望对你有一定的参考价值。
一、instanceof 运算符用法:
instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
public class Temp2 { interface A { } static class B implements A { } static class C extends B { } public static void main(String[] args) { A a = null; B b = null; boolean res; System.out.println("instanceoftest test case 1: ------------------"); res = a instanceof A; System.out.println("a instanceof A: " + res); res = b instanceof B; System.out.println("b instanceof B: " + res); System.out.println("/ninstanceoftest test case 2: ------------------"); a = new B(); b = new B(); res = a instanceof A; System.out.println("a instanceof A: " + res); res = a instanceof B; System.out.println("a instanceof B: " + res); res = b instanceof A; System.out.println("b instanceof A: " + res); res = b instanceof B; System.out.println("b instanceof B: " + res); System.out.println("/ninstanceoftest test case 3: ------------------"); B b2 = (C) new C(); res = b2 instanceof A; System.out.println("b2 instanceof A: " + res); res = b2 instanceof B; System.out.println("b2 instanceof B: " + res); res = b2 instanceof C; System.out.println("b2 instanceof C: " + res); } /* result: instanceoftest test case 1: ------------------ a instanceof A: false b instanceof B: false instanceoftest test case 2: ------------------ a instanceof A: true a instanceof B: true b instanceof A: true b instanceof B: true instanceoftest test case 3: ------------------ b2 instanceof A: true b2 instanceof B: true b2 instanceof C: true */ }
二、
以上是关于java 零散知识汇总(初级)的主要内容,如果未能解决你的问题,请参考以下文章