Java-オブジェクトタイプ変換とinstanceof関数
正直本当に回りくどいです...
/*
*
* ,
* ,
* 。 ,
* , ,
*
*
* */
class GrandS extends Father{
public static void main(String[] args){
// a1 ,a2
GrandF a1 = new Father();
GrandF a2 = new GrandS();
// , , ‘ ’
Father b1 = (Father) a1;
Father b2 = (Father) a2;
// GrandS c1 = (GrandS) a1; // turn Father to GrandS,
GrandS c2 = (GrandS) a2;
// instanceof .
if(b1 instanceof GrandF) System.out.println("b1 belongs GrandF");
if(a1 instanceof Father) System.out.println("a1 belongs Father");
if(a1 instanceof GrandS) System.out.println("a1 belongs GrandS");
else System.out.println("a1 does not belong GrandS");
if(a2 instanceof GrandS) System.out.println("a2 belongs GrandS");
if(c2 instanceof GrandF) System.out.println("c2 belongs GrandF");
}
}
class Father extends GrandF {
}
class GrandF {
}