JAVA实例数目 设计的一个类只允许创建一个实例,如何写出类的框架代码。设计的类最多只能创建3个实例,如何修改代码?设计一个类,在程序运行时才决定它最多能创建的实例个数,这又如何设计它? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package test;import java.util.Random;/** * 一个的 */class Test { private static Test test = new Test(); private Test() { } public static Test getTest() { return test; }}/** * 三个的 */class Test2 { private static Test2[] tests = new Test2[3]; private Test2() { } public static Test2 getTest2() { return tests[new Random().nextInt() % 3]; } public static Test2[] getTest2s() { return tests; }}/** * N个的 */class Test3 { private static Test3[] tests = null; private static int classNum; private static boolean isInit = false; private Test3() { } public static void init(int objectNum) { if (!isInit) { tests = new Test3[objectNum]; classNum = objectNum; } } public static Test3 getTest3() { if (tests == null) { throw new RuntimeException("请先调用init方法初始化"); } return tests[new Random().nextInt() % classNum]; } public static Test3[] getTest3s() { if (tests == null) { throw new RuntimeException("请先调用init方法初始化"); } return tests; }}乱盖的.. 不过调用的时候才说明生成几个对象 应该用工厂什么的去完成..没时间了 随便误导下人吧 (大家自信点)是大牛的请进,有关 对象为 NULL 的判断。 你看看就知道了 jxl java设置excel单元格中单个字符的颜色《急》 jsp生成的servlet中文乱码怎么解决?急需高手解决!!! 如何部署扩展的功能 spring配置文件错误,郁闷死了,高手帮帮忙 JAVA有没有System.Web.UI.WebControls类似的类 CMP2.0怎么实现灵活查询? 关于Struts中页面校验的问题。请大家帮忙讨论。 socket 在xp和win2k之间的数据传输问题 帮忙看看这句话是怎么回事呀? 如何用EJB3.0实现文件上传与下载 NetBeans开发web services出错
package test;import java.util.Random;/**
* 一个的
*/
class Test {
private static Test test = new Test(); private Test() { } public static Test getTest() {
return test;
}
}/**
* 三个的
*/
class Test2 {
private static Test2[] tests = new Test2[3]; private Test2() { } public static Test2 getTest2() {
return tests[new Random().nextInt() % 3];
} public static Test2[] getTest2s() {
return tests;
}
}/**
* N个的
*/
class Test3 {
private static Test3[] tests = null;
private static int classNum;
private static boolean isInit = false; private Test3() { } public static void init(int objectNum) {
if (!isInit) {
tests = new Test3[objectNum];
classNum = objectNum;
}
} public static Test3 getTest3() {
if (tests == null) {
throw new RuntimeException("请先调用init方法初始化");
}
return tests[new Random().nextInt() % classNum];
} public static Test3[] getTest3s() {
if (tests == null) {
throw new RuntimeException("请先调用init方法初始化");
}
return tests;
}
}乱盖的.. 不过调用的时候才说明生成几个对象 应该用工厂什么的去完成..没时间了 随便误导下人吧