在泛型类中创建泛型方法名称? [英] Creating generic method names in generic class?

查看:154
本文介绍了在泛型类中创建泛型方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class MyHolder< T> {
私人T值;

public MyHolder(T t){
this.value = t;
}

public T getValue(){
first first;
}

public void setValue(T t){
this.first = t;






$ b

有了这个,我可以像这样使用它: -

  MyBean bean = new MyBean(); 
MyHolder< MyBean> obj = new MyHolder< MyBean>(bean);
obj.getValue(); //返回bean

不要将getter / setter调用为 getValue ) setValue(..),是否有可能生成呢?

从本质上来说,将它放在 getMyBean() setMyBean(。 。),取决于传入的类型。当然,这是一个非常简单的例子,但是如果我创建一个通用持有者类,并且带有N个通用属性,那么将它称为有意义的 getValue1() getValue2()等。



谢谢。

解决方案

不。 Java中没有这样的功能。我甚至无法想象它是如何在语法上看起来的...... void set< T>(); ?那么getter / setter对于例如 MyHolder< ;?扩展Number> 看看?


Currently, I have something like this:-

public class MyHolder<T> {
    private T   value;

    public MyHolder(T t) {
        this.value = t;
    }

    public T getValue() {
        return first;
    }

    public void setValue(T t) {
        this.first = t;
    }
}

With this, I can use it like this:-

MyBean bean = new MyBean();
MyHolder<MyBean> obj = new MyHolder<MyBean>(bean);
obj.getValue(); // returns bean

Instead of calling the getter/setter to be getValue() and setValue(..), is it possible to "generify" that too?

Essentially, it would be nice to have it getMyBean() and setMyBean(..), depending on the type passed in. Granted this is a very simple example, however if I create a generic holder class that takes N generic properties, then it would be nice to call it something meaningful instead of getValue1() or getValue2(), and so on.

Thanks.

解决方案

No. There is no such feature in Java. I can't even imagine how it would look syntactically... void set<T>();? And how would the getter / setter for for instance MyHolder<? extends Number> look?

这篇关于在泛型类中创建泛型方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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