首页 > 其他 > 详细

有时我们需要调用一个函数时,返回多个不同类型的数据

时间:2015-05-30 14:50:59      阅读:182      评论:0      收藏:0      [点我收藏+]

有时我们需要调用一个函数时,返回多个不同类型的数据

 

使用Object[]存储:

 

public class Print {

    public static void main(String[] args) {
        Object[] array = returnAObject();
        String a = (String) array[0];
        Struct b = (Struct) array[1];
        int i = (Integer) array[2];
        System.out.println(a);
        System.out.println(b.s);
        System.out.println(i);
    }

    public static Object[] returnAObject() {
        Object[] array = new Object[4];
        //对象数组现在存放不同类型的数据.
array[0] = "abc";
        array[1] = new Struct();
        array[2] = 3;
        return array;
    }

}

class Struct {//随便写一个类
    public int a = 3;
    public String s = "Hello World!";

    public Struct() {//安全留空

    }
}

 

有时我们需要调用一个函数时,返回多个不同类型的数据

原文:http://www.cnblogs.com/jiahuafu/p/4540274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!