//===============第1个类====================
public class LoadingInformation extends Thread {
static public Vector friendID=new Vector();
static public int friendno;
```
``
public LoadingInformation(){
this.start();
}public void run(){
friendno=dis.readInt();
while(friendno>0){
friendID.addElement(dis.readUTF());
friendno--;
}
}
//===========第2个类================
public class List1{
public FriendList() {
System.out.println("number "+LoadingInformation.friendno)
for(int i=0;i<LoadingInformation.friendno;i++){
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
friendName[i]=LoadingInformation.friendID.elementAt(i).toString();
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
}
}}
我在第2个类中要调用第1个类的值,我第1个类中的 Vector friendID和 friendno都已设成了Static.我想在第2个类中共享第1个类的值, 但是在第2个类中引用时候测试出friendno=0和在第1个类的值不同(第1个类中friendno的值为3),怎么回事 还望高手指点
public class LoadingInformation extends Thread {
static public Vector friendID=new Vector();
static public int friendno;
```
``
public LoadingInformation(){
this.start();
}public void run(){
friendno=dis.readInt();
while(friendno>0){
friendID.addElement(dis.readUTF());
friendno--;
}
}
//===========第2个类================
public class List1{
public FriendList() {
System.out.println("number "+LoadingInformation.friendno)
for(int i=0;i<LoadingInformation.friendno;i++){
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
friendName[i]=LoadingInformation.friendID.elementAt(i).toString();
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
}
}}
我在第2个类中要调用第1个类的值,我第1个类中的 Vector friendID和 friendno都已设成了Static.我想在第2个类中共享第1个类的值, 但是在第2个类中引用时候测试出friendno=0和在第1个类的值不同(第1个类中friendno的值为3),怎么回事 还望高手指点
解决方案 »
- 关于myeclipse的问题
- 求救:使用spring的JdbcTemplate执行batchUpdate时出现数组越界
- 怎么把一个FileOutputStream变成OutputStream啊。
- laf 的问题
- java连接sqlserver问题java.sql.SQLException: No suitable driver
- 跪求两个java小程序
- 请教在职程序员......
- [急]如何定时捕捉视频窗口中图像,并以JPEG的格式把它保存在本地机C:temp\目录下
- 一个java初学者的小问题,请各位指点一下。
- 谁是高手谁来看看这两道题怎么回事情
- 怎样使用JBuilder2005把一个application程序转化为exe应用程序
- 请教大家,我想通过JDBC访问一台安全性设置为Windows验证的SQL Server,请问如何写创建数据库连接的程序呀?
//===========第2个类================
public class FriendList{
public FriendList() {
System.out.println("number "+LoadingInformation.friendno)//这里不会执行第一个类的run
LoadingInformation loadingInformation = new LoadingInformation();
for(int i=0;i<LoadingInformation.friendno;i++){
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
friendName[i]=LoadingInformation.friendID.elementAt(i).toString();
System.out.println(LoadingInformation.friendID.elementAt(i).toString());
}
}}
要定义一个对象,激发LoadingInformation的构造函数才成.
调用静态成员,只初始化静态成员,并不调用构造函数
比如说friendno从dis.readInt()的值是3,那么在第2个类中的System.out.println("number "+LoadingInformation.friendno)点输出的也应该是3,但是现在输出的是0.
现在要求第2个类可以new第1个类,但是第1个类不能new第2个类,请问下高手应该怎么做才能保证第1个类的值能传递到第2个类中.
friendno=dis.readInt();
while(friendno>0){
friendID.addElement(dis.readUTF());
friendno--; //执行之后friendno为0
}
改为:public void run(){
friendno=dis.readInt();
int count = friendno; //添加记录数字
while(count>0){
friendID.addElement(dis.readUTF());
count--; //执行之后,count为0
}
public class LoadingInformation extends Thread {
private Vector friendID=new Vector();
private int friendno; public LoadingInformation(){
} public void run(){
friendno=dis.readInt();
int count = friendno;
while(count >0){
friendID.addElement(dis.readUTF());
count --;
}
} public int getFriendNo(){
return friendno;
}
public Vector getFriendId(){
return friendID;
}
}
//===========第2个类================
public class FriendList{
public FriendList() {
LoadingInformation loadingInformation = new LoadingInformation();
loadingInformation .start();
int friendNumber = loadingInformation.getFriendNo();
String[] friendName = new String[friendNumber];
Vector temp = loadingInformation.getFriendId();
for(int i=0;i<friendNumber;i++){
System.out.println(temp.elementAt(i).toString());
friendName[i]=temp.elementAt(i).toString();
}
}
}