如何判断一个类是否继承自另一个类

windsoft 2006-02-13 01:09:44
如何判断一个类是否继承自某个类
包括继承自某个类的子类的子类....
也就是判断一个类是否是某个类的儿子、孙子或是孙孙子....
...全文
1841 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
windsoft 2006-02-16
  • 打赏
  • 举报
回复
如何反射呢
有没有例子将field转换成本身的类
紫炎圣骑 2006-02-14
  • 打赏
  • 举报
回复
同意楼上
weinickli 2006-02-14
  • 打赏
  • 举报
回复
反射是正解
gifted1982 2006-02-14
  • 打赏
  • 举报
回复
if (A instanceof B) {}
windsoft 2006-02-14
  • 打赏
  • 举报
回复
谁知道field和instanceof如何结合
for(Field objItem : this.getClass().getDeclaredFields()){
System.out.println("field:" + objItem.getName());
try{
System.out.println(Class.forName(objItem.getType().getName()).getName());
if ((Object) objItem instanceof Controls) {
System.out.println(":true");
}
}catch(Exception e){
e.printStackTrace();
}
}
网络咖啡 2006-02-14
  • 打赏
  • 举报
回复
使用反射可以实现判别
jiangshachina 2006-02-14
  • 打赏
  • 举报
回复
我觉得应该使用RTTI(运行时类型识别),也就是用Reflect。
universe01 2006-02-14
  • 打赏
  • 举报
回复
你的情况应该需要反射
interpb 2006-02-14
  • 打赏
  • 举报
回复
楼主只是想做一个运行时的型别检查可以用三种方法
1. A instanceof B
2. B.Class.isInstance(object o)
3.比较两个对象的Class对象
还有反射稍微复杂一点
cnyxlxw 2006-02-13
  • 打赏
  • 举报
回复
instanceof
congliu 2006-02-13
  • 打赏
  • 举报
回复
instanceof
wmzsl 2006-02-13
  • 打赏
  • 举报
回复
instanceof
M3tdw2A7tVe8Xxhd 2006-02-13
  • 打赏
  • 举报
回复
instanceof
yuzl32 2006-02-13
  • 打赏
  • 举报
回复
instanceof
windsoft 2006-02-13
  • 打赏
  • 举报
回复
for(Field objItem : this.getClass().getDeclaredFields()){
System.out.println("field:" + objItem.getName());
try{
System.out.println(Class.forName(objItem.getType().getName()).getName());
if ((Object) objItem instanceof Controls) {
System.out.println(":true");
}
}catch(Exception e){
e.printStackTrace();
}
}
windsoft 2006-02-13
  • 打赏
  • 举报
回复
我想判断类中所定义的属性是否是某个类,不过好象不行

for(Field objItem : this.getClass().getDeclaredFields()){
this.getPageParameters().getWriter().println("field:" + objItem.getName());
try{
this.getPageParameters().getWriter().println(Class.forName(objItem.getType().getName()).getName());
if ((Object) objItem instanceof Controls) {
this.getPageParameters().getWriter().println(":true");
}
}catch(Exception e){
e.printStackTrace();
}
this.getPageParameters().getWriter().println("<BR>");
}
chg2008 2006-02-13
  • 打赏
  • 举报
回复
instanceof
trumplet 2006-02-13
  • 打赏
  • 举报
回复
ChildClass c;
ParentsClass p;
......


if ( c instanceof p )
{
....
}
windsoft 2006-02-13
  • 打赏
  • 举报
回复
up

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧