非常简单的Java动态铸造 [英] Very simple Java Dynamic Casting

查看:112
本文介绍了非常简单的Java动态铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但我花了一个多小时。我的代码如下。我需要使SomeClass sc动态。所以你传递类名称作为一个字符串在一个函数,并使用该类代替static someClass。怎么去呢?

  SomeClass sc; 
if(someOtherClassObject instanceof SomeClass){
sc =(SomeClass)someOtherClassObject;

我想要的是

  public void castDynamic(String strClassName){
//将由strClassName引用的类名称转换为SomeClass
//如果它是SomeClass的实例
}

编辑:
上面是简化。实际代码是

  public void X(String className,RequestInterface request)
{
//我现在的代码是这样,我需要改变,使XRequest
//可以是由className引用的任何类,
//和request.getRequest属于className class
//我不想要静态XRequest xvr,它应该动态获取

XRequest xvr;
if(request.getRequest()instanceof XRequest){
xvr =(XRequest)request.getRequest();
client.setRequest(xvr);
}
}

另一个简单的说法: getRequest()。我没有线索这个对象是什么。所以我需要把它转换为提供的类字符串名称。怎么做?就这样。 - SQC 13分钟前

解决方案

您想要按类名实例化 >

首先,您需要创建一个 Class<?> 对象:

 类<?> cls = Class.forName(strClassName); 

然后实例化这个(注意,这可以抛出各种异常 - 访问冲突,

 对象实例(ObjectNotFound  = cls.newInstance(); 

然后您就可以投放:

  return(SomeClass)instance; 

请确保您了解以下两者之间的差异:


  1. 名称(约为文件名)

  2. 对象 信息)

  3. 实例(此类型的实际对象)

您还可以将 cls 对象转换为类型 Class< ;?扩展SomeClass> ,如果你想。它不会给你太多,但。您可以内联它,以:

  return(SomeClass)(Class.forName(strClassName).newInstance()); 

哦,但您可以 c> cls 对象,然后实例化它。



编辑:添加进一步的示例代码,向反射

strong>。



例如:

  if(cls.isInstance (request)){
// ...
}

方法,您需要知道一个可以转换的接口,或者使用反射 getMethod cls object):

 方法getRequest = cls.getMethod(getRequest); 
getRequest.invoke(request);


Simple question but I have spent over an hour with this. My code is below. I need to make SomeClass sc dynamic. So you pass the class name as a string in a function and use that class in place of static someClass. How to go about it?

SomeClass sc;
if (someOtherClassObject instanceof SomeClass){
    sc=(SomeClass) someOtherClassObject;

What I want is

public void castDynamic (String strClassName){
  //cast the classname referred by strClassName to SomeClass 
  //if it is the  instance of SomeClass
}

EDIT: The above was simplification. The actual code is this

public void X(String className, RequestInterface request)
{
    //My current code is this, I need to change so that "XRequest"
    //can be any   class referred by "className", 
    //and "request.getRequest" the object belonging to "className" class
    //I don't want static XRequest xvr, it should be fetched dynamically

    XRequest xvr;
    if (request.getRequest() instanceof XRequest){
        xvr=(XRequest) request.getRequest();
        client.setRequest(xvr); 
    }
}

Another simple rephrase: I get an object using request.getRequest(). I have no clue what that object is. So I need to cast it to the classstring name provided. How to do that? That's all. – SQC 13 mins ago

解决方案

You want to instantiate a class by it's name?

First of all, you need to make a Class<?> object:

Class<?> cls = Class.forName(strClassName);

Then instantiate this (note, this can throw various exceptions - access violation, ClassNotFound, no public constructor without arguments etc.)

Object instance = cls.newInstance();

Then you can cast it:

return (SomeClass) instance;

Please make sure you understand the differences between:

  1. A class name (approximately a file name)
  2. A class object (essentially a type information)
  3. A class instance (an actual object of this type)

You can also cast the cls object to have type Class<? extends SomeClass>, if you want. It doesn't give you much, though. And you can inline it, to:

return (SomeClass)(Class.forName(strClassName).newInstance());

Oh, but you can do type checking with the cls object, before instantiating it. So you only instanciate it, if it satisfies your API (implements the interface you want to get).

EDIT: add further example code, towards reflection.

For example:

if (cls.isInstance(request)) {
  // ...
}

For invoking methods, you either need to know an interface that you can cast to, or use reflection (the getMethod methods of the cls object):

Method getRequest = cls.getMethod("getRequest");
getRequest.invoke(request);

这篇关于非常简单的Java动态铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆