方法重载 [英] Method overloading

查看:73
本文介绍了方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你是否可以在这里提出一些建议.

I was wondering if you can suggest something here.

我想要两种方法:

doSomething(List<Data>) and
doSomething(List<Double>)

由于参数类型相同,Java报错

Since type of parameter is the same, Java is complaining

有没有办法以某种方式使这种重载发生?

Is there a way to somehow make this overloading happen?

推荐答案

public void doSomething(List list) {
    if(list.size() > 0) {
        Object obj = list.get(0);
        if(obj instanceof Data) {
           doSomethingData((List<Data>)list);
        } else if (obj instanceof Double) {
           doSomethingDouble((List<Double>)list);
        }
    }
}

这篇关于方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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