java中instanceof是怎麽用的, 幹什麽使的,舉例!
instanceof關鍵字 (視頻下載) (全部書籍)
instanceof是java中固有的關鍵字, 就像main, public一樣,用法:aa instanceof AA 就是問aa是不是AA的一個實例, 是的話,就返回真。馬 克 - t o - w i n:當用instance of測試時,馬克-to-win:子類的指針是一個instance of父類, 返回值為真,見以下的例子。
例1.9.2---本章源碼
class AMark_to_win {
}
class B {
}
class AA extends AMark_to_win {
}
public class Test {
public static void main(String[] args) {
AMark_to_win a = new AMark_to_win();
AMark_to_win aa = new AA();
B b = new B();
AA aaa = (AA) aa;
AA aaaa = new AA();
System.out.println(a instanceof AA);// false
System.out.println(aa instanceof AA);// true
System.out.println(a instanceof AMark_to_win);// true
System.out.println(b instanceof B);// true
System.out.println(aa instanceof AMark_to_win);// true//think of AMark_to_win aa=nul;馬克-to-win:
System.out.println(aaa instanceof AA);// true
/* true aaa is an instance of AA,AA is a A, (is relationship. ) */
System.out.println(aaa instanceof AMark_to_win);
。。。。。。。。。
詳情請見:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner3_web.html#SubclassInstance
java中instanceof是怎麽用的, 幹什麽使的,舉例!