由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 这道题该走什么路
相关主题
A question about inheritanceCan Java thread return a value?
关于new operator的问题使用object tag如何让IE和firefox兼容applet?
怎样用class的string type name 动态生成object? 请问如何同时启动多个进程并等待
问一个关于access object instance的问题synchronized method does lock the object that passed into the method as a parameter?
Re: connection pool关于Random怎么用?
how to print the cllass name of an instance?问个面试题, 谢谢
Re: 谁有Java或Oracle的毒招 ?一个关于generics的问题
Help: 小程序挑错CC150 16.6答案是不是有问题? (转载)
相关话题的讨论汇总
话题: someclass话题: obj话题: collie话题: param话题: class
进入Java版参与讨论
1 (共1页)
o****h
发帖数: 58
1
不用 instanceof operator 或 the isInstance() method
怎样测出某个instance是不是某个class的instance
Your idea is appreciated
p*****e
发帖数: 5165
2

call getClass().getName() and compare to class name
This may work only for public classes

【在 o****h 的大作中提到】
: 不用 instanceof operator 或 the isInstance() method
: 怎样测出某个instance是不是某个class的instance
: Your idea is appreciated

b**o
发帖数: 16
3
SomeClass obj;
Object param;
try{
obj = (SomeClass) param;
System.out.println("Yes, it is a instance of SomeClass");
}catch(ClassCastException e){
System.out.println("No, it isn't.");
}


【在 o****h 的大作中提到】
: 不用 instanceof operator 或 the isInstance() method
: 怎样测出某个instance是不是某个class的instance
: Your idea is appreciated

o****h
发帖数: 58
4

//要求做成一个method, 于是,偶这样
class Collie extends Dog{
public boolean isa(String className){ //需要全路径,如java.lang.Object
Collie param=new Collie();
try {
Class SomeClass = Class.forName(className);
// System.out.println(SomeClass);
Object obj = SomeClass.newInstance();
obj=(SomeClass)param; // 这里总是有问题呢,该怎么办 ???
return true;}
catch(Exception e){return false;}
}
}
另外一种方法倒是没问题,并且不必输入全路径,但好像烦了很多
class Collie extends Dog{
public boolean isa(String className){


【在 b**o 的大作中提到】
: SomeClass obj;
: Object param;
: try{
: obj = (SomeClass) param;
: System.out.println("Yes, it is a instance of SomeClass");
: }catch(ClassCastException e){
: System.out.println("No, it isn't.");
: }
:

1 (共1页)
进入Java版参与讨论
相关主题
CC150 16.6答案是不是有问题? (转载)Re: connection pool
array 在java里 是一定放在heap 吗?how to print the cllass name of an instance?
请教一个多线程的问题Re: 谁有Java或Oracle的毒招 ?
question about GUIHelp: 小程序挑错
A question about inheritanceCan Java thread return a value?
关于new operator的问题使用object tag如何让IE和firefox兼容applet?
怎样用class的string type name 动态生成object? 请问如何同时启动多个进程并等待
问一个关于access object instance的问题synchronized method does lock the object that passed into the method as a parameter?
相关话题的讨论汇总
话题: someclass话题: obj话题: collie话题: param话题: class