码迷,mamicode.com
首页 > 编程语言 > 详细

Java之instanceof

时间:2015-09-22 14:23:59      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

class Base{

????int x = 1;

????static int y = 2;

????String name(){

????????return "mother";

????}

????static String staticname(){

????????return "static mother";

????}

}

class Subclass extends Base{

????int x = 4;

????int y = 5;

????int z = 6;

????String name(){

????????return "baby";

????}

????static String staticname(){

????????return "static baby";

????}

}

public class Test02{

????public static void main(String[] args){

????????Subclass s = new Subclass();

????????if(s instanceof Subclass){

????????????System.out.println(s.x+" "+s.y+" "+s.name()+" "+s.staticname());

?

????????}

????????

????????Base s1 = s;

????????if(s1 instanceof Subclass){

????????????System.out.println(s1.x+" "+s1.y+" "+s1.name()+" "+s1.staticname());

????????

????????}

????????

????????Base s2 = new Base();

????????if(s2 instanceof Subclass){

????????????System.out.println(s2.x+" "+s2.y+" "+s2.name()+" "+s2.staticname());

????????

????????}

????????

????}

}

/*

结果:

4 5 baby static baby

1 2 baby static mother

?

结论:

instanceof 用来判定对象A是否为类B的对象,或者A,B之间是否存在继承关系

如果A,B之间是不否存在继承关系,则编译不过

?

*/

Java之instanceof

标签:

原文地址:http://www.cnblogs.com/iucforever/p/4828608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!