Java相当于C#动态类类型? [英] Java equivalent to C# dynamic class type?

查看:437
本文介绍了Java相当于C#动态类类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在C#中我可以使用类动态 http://msdn.microsoft.com/en-us/library/dd264741.aspx



这样可以让我不必使用模板/泛型类,但可以为证书的情况实现简单的感觉。



我已经不成功互联网搜索不幸的是动态和java关键字在动态架构上引起了许多无关的信息。



我已经在javaFX中玩过一些,并且有一个类似于var的类似于c#的动态相同的用法。但是它似乎在Java中似乎不可用。



谢谢,
stephanie

解决方案

Java不支持动态输入,但您可以使用Java中的动态代理模拟类似的东西。首先,您需要在对象上声明一个需要调用操作的界面:

  public interface MyOps {
void foo();
void boo();
}

然后在myObjectInstance上创建动态调用代理:

  MyOps p =(MyOps)Proxy.newProxyInstance(getClass()。getClassLoader(),// 
new Class<?> [] MyOps.class},//
new MyHandler(myObject));
p.foo();
p.boo();

其中MyHandler的声明如下:

  public class MyHandler实现InvocationHandler {
private final Object o;

public MyHandler(Object o){
this.o = o;


public Object invoke(Object proxy,Method m,Object [] args)throws Throwable {
Method method = o.getClass()。getMethod(m.getName ),m.getParameterTypes());
return method.invoke(o,args);
}
}

所以,如果myObject有方法foo()和boo (),它们将被调用,否则你会得到一个RuntimeException。



还有可以在JVM中运行的支持动态输入的语言数量,例如Scala,Groovy,JRuby,BeanShell,JavaScript / Rhino等等。 Java 7中有一些JVM更改将支持本机动态调度,因此这些语言可以执行得更好,但这种功能不会直接暴露在静态类型的Java语言中。


I'm coming from the world of c#.

in C# i am able to use the class dynamic http://msdn.microsoft.com/en-us/library/dd264741.aspx

This allows me to not have to use templated/generic classes but achieve a simliar feel for certian situations.

I'm have been unsuccessfull in internet searchs as unfortunately 'dynamic' and 'java' keywords turn up alot of unrelated infromation on dynamic architectures.

I have dabbled a bit in javaFX and there is a type var which appears to have the same usage as c#'s dynamic. However it doesnt appear to be usable in Java.

thanks, stephanie

解决方案

Java doesn't support dynamic typing, but you can simulate something like that using dynamic proxy in Java. First you'll need to declare an interface with operations you want to invoke on your objects:

public interface MyOps {
  void foo();
  void boo();
}

Then create Proxy for dynamic invocation on myObjectInstance:

MyOps p = (MyOps) Proxy.newProxyInstance(getClass().getClassLoader(), //
    new Class<?>[] { MyOps.class }, //
    new MyHandler(myObject));
p.foo();
p.boo();

where MyHandler is declared like this:

public class MyHandler implements InvocationHandler {
  private final Object o;

  public MyHandler(Object o) {
    this.o = o;
  }

  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Method method = o.getClass().getMethod(m.getName(), m.getParameterTypes());
    return method.invoke(o, args);
  }
}

so, if myObject has methods foo() and boo(), they will be invoked, or else, you'll get a RuntimeException.

There is also number of languages that can run in JVM support dynamic typing, e.g. Scala, Groovy, JRuby, BeanShell, JavaScript/Rhino and many others. There is some JVM changes are coming in Java 7 to support a native dynamic dispatch, so these languages could perform much better, but such feature won't be directly exposed in statically typed Java language.

这篇关于Java相当于C#动态类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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