Java如何实现一个循环队列保存另一个类的对象

Java如何实现一个循环队列保存另一个类的对象,第1张

这里说下我的思路。循环队列。首先你要想清楚队列。队列有多种实现方式。链表和数组。
如果是固定的个数那可以用数组实现否则用双向循环链表实现。 具体说实现方式可以参考link list或arrayDequeue

你启动了两个类里的两个main函数了吗?
当你启动了ListenerImpl 类里的main函数时相当于启动了一个程序,
当你启动了TopicImpl类里的main函数时又启动了一个程序,
一个类里的静态变量只是在一个程序里面只有一个实体,你这启动的是两个程序,所以TopicImpl实体是两个完全不同的实体,所以在TopicImpl的main函数中取到的TopicImpl实体中的list肯定是空的,你一直没有加ListenerImpl 类的实体进去,所以肯定就没有输出了。
你应该把ListenerImpl 类里的main函数去掉,直接在TopicImpl类的main函数中新建ListenerImpl 实体,并把它添加到TopicImpl类的list当中!

最简单的,你先建立一个对象数组
JButton[] buttons = null;
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton();
}
然后再对数组中单个的对象进行相关 *** 作,代码你可以直接粘贴
顺便想问下楼上的,你那循环创建的是几个对象啊???
for(int i=0;i<=11;i++)
{
String s=StringvalueOf(i);
p=new Button(s);
paddActionListener(this);
ppadd(p);
}
搂主要的是很多的对象,而你虽然循环了10次,但只实例化了一个……

 public void temp() throws Exception{
//1获得class
       //获得类的当前实例
       Actions a = this;
       Systemoutprintln(a);
       //输出 comtaobaotuisiActions$$EnhancerByCGLIB$$dff89711@c2ccac
       //获得指定类的新实例
       Actions b = ActionsclassnewInstance();
       Systemoutprintln(b);
       //输出 comtaobaotuisiActions@1e4fede
//
       //通过类型获得类
       Class boolType = Booleanclass;
       Systemoutprintln(boolType);
       //输出 class javalangBoolean
//
       //通过变量获得类
       String stringExample = “”;
       Class stringType = stringExamplegetClass();
       Systemoutprintln(stringType);
       //输出class javalangString
//
       //由名字获得类
       Class<> c = ClassforName(“comtaobaotuisiActions”);
       Systemoutprintln(c);
       //输出 class comtaobaotuisiActions
//
//2关于method
       //由函数名和参数类型得到函数
       Long userId = 9999l;
       Method method = ActionsclassgetDeclaredMethod(“tempMethod”, userIdgetClass());
       Systemoutprintln(method);
       //输出 public void comtaobaotuisiActionstempMethod(javalangLong)
//
       //通过类、参数值调用指定函数
       Actions actions = new Actions();
       Long args[] = new Long [1];
       args[0] = userId;
       methodinvoke(actions, args);
       //输出  我是JAVA反射测试方法,我被invoke了9999
    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13387094.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-26
下一篇 2023-07-26

发表评论

登录后才能评论

评论列表(0条)

保存