代码之家  ›  专栏  ›  技术社区  ›  pandoragami

在运行时创建对象并将数据存储在数组中的包装器类

  •  0
  • pandoragami  · 技术社区  · 16 年前

    我试着创建一个封装对象的包装类、一个字符串(用于命名和区分对象实例)和一个存储数据的数组。我现在遇到的问题是使用确定对象“名称”的方法访问这个类,同时读取包含一些随机变量的数组。

    注意:这是从原来的编辑,但下面的代码无论如何是更好的。

    import java.util.Arrays;
    import java.util.Random;
    public class WrapperClass
    {
        String varName;
        Object varData;
        int[] array = new int[10];
        static WrapperClass globalobject;
        public WrapperClass(String name, Object data, int[] ARRAY)
        {
            varName = name;
            varData = data;
            array = ARRAY;
        }
        public static void getvalues()
        {
    
        }
        public static void main(String[] args)
        {
    
           Random random = new Random(3134234);
           String x;
           Object y;
           int[] n = new int[10];
           WrapperClass object;
           for(int i = 0; i < 10; i++)
           {
    
             int[] array = new int[10];
    
            for (int c = 0; c < 10; c++)
            {
                array[c] = random.nextInt();//randomly creates data
            }
            globalobject = new WrapperClass("c" + i, new Object(),array);
    
           } 
           globalobject.getvalues();
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Dan Story    16 年前

    好吧,我继续看了你在评论中链接的帖子,现在我想我明白你为什么要这么做了。这似乎是一个沟通问题,在很大程度上——我想你已经知道你在试图从概念上做什么,但从我的了解来看,你对OOP还不太熟悉,很难用技术术语来表达它。

    本质上,你想做的是获取某种类型的任意数据,用一个名字把它填充掉,然后在将来用这个名字把它取回,对吧?

    Java有一个名为 地图 这样就可以做到这一点,而无需担心包装或其他结构。这里有一个关于如何使用地图的评论很重的例子,希望它能帮助你澄清一些困惑:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Random;
    
    class MyClass
    {
        public String name;
        public int[] myIntArray;
    
        public MyClass()
        {
            myIntArray = new int[10];
        }
    }
    
    public class Program
    {
        static final int MAX_RANDOM = 20;
    
        public static void main(String[] args)
        {
            // Create a new map which will use keys of type String and
            // hold values of type MyClass.
            HashMap<String, MyClass> map = new HashMap<String, MyClass>();
    
            // Initialize a new random object. Not supplying a seed number
            // means that it seeds from the system clock, a good source of
            // randomness.
            Random rand = new Random();
    
            // Loop ten times...
            for (int i = 0; i < 10; ++i)
            {
                // ...and each time, create a new MyClass and call it "m"...
                MyClass m = new MyClass();
    
                // ...and set m.name as "c" followed by the iteration number.
                m.name = "c" + i;
    
                // Loop as many times as there are elements in m.myIntArray...
                for (int j = 0; j < m.myIntArray.length; ++j)
                {
                    // ...and each time, set the next element to a new random int
                    // with a value between 0 and MAX_RANDOM.
                    m.myIntArray[j] = rand.nextInt(MAX_RANDOM);
                }
    
                // Once we've filled all the variables, push the object into
                // the map using its name as the lookup key.
                map.put(m.name, m);
    
                // The "m" variable now goes out of scope and the name can be
                // re-used on the next iteration of the loop. "map" and "rand"
                // stay in scope because they were declared outside the loop body.
            }
    
            // Now that we've created ten objects, time to read them back out.
    
            // Get an iterator object which will let us traverse the set of
            // keys (names) in the map.
            Iterator<String> i = map.keySet().iterator();
            // While i still has more keys in the set...
            while (i.hasNext())
            {
                // Grab the next name from the iterator and assign it to
                // a local variable called "name".
                String name = i.next();
    
                // Fetch the instance of MyClass from the map that corresponds
                // to the key (name) we've just grabbed.
                MyClass m = map.get(name);
    
                // Print its name to the console...
                System.out.println("Name: " + m.name);
    
                // Print an opening bracket for the array...
                System.out.print("[");
                // ...and now run through each of the integers in the array
                // and print those out, too.
                for (int j = 0; j < m.myIntArray.length; ++j)
                {
                    // Print the integer at position j...
                    System.out.print(m.myIntArray[j]);
                    // ...and if it's not the last item in the array,
                    // print a comma to separate it from the next one.
                    if (j < m.myIntArray.length - 1) System.out.print(", ");
                }
                // Print the closing bracket, followed by two newlines to
                // separate the next class with whitespace.
                System.out.println("]\n");
            }
        }
    }
    
        2
  •  0
  •   Wajdy Essam    16 年前

    您希望封装对象,但您的成员字段封装得不好,请将它们设为私有并添加setter和getter方法,