当前位置: 技术问答>java相关
在一个类里声明了一个Vector 我想在别的类里访问它,对他进行操作,我应该怎么做
来源: 互联网 发布时间:2015-06-25
本文导语: 急请各位帮忙 | public class userManager{ private Hashtable hashUsers; public userManager() { hashUsers = new Hashtable(); } public void putUserInfo(String userKey,String userMa...
急请各位帮忙
|
public class userManager{
private Hashtable hashUsers;
public userManager() {
hashUsers = new Hashtable();
}
public void putUserInfo(String userKey,String userManager) {
if (hashUsers.containsKey(userKey)==null) {
Vector vec = new Vector();
vec.add(userManager);
hashUsers.put(userKey,vec);
} else {
Vector vec = (Vector)hashUsers.get(userKey);
hashUsers.put(userKey,vec);
}
}
public Vector getUserInfo(String userKey) {
return hashUsers.get(userKey);
}
}
private Hashtable hashUsers;
public userManager() {
hashUsers = new Hashtable();
}
public void putUserInfo(String userKey,String userManager) {
if (hashUsers.containsKey(userKey)==null) {
Vector vec = new Vector();
vec.add(userManager);
hashUsers.put(userKey,vec);
} else {
Vector vec = (Vector)hashUsers.get(userKey);
hashUsers.put(userKey,vec);
}
}
public Vector getUserInfo(String userKey) {
return hashUsers.get(userKey);
}
}
|
将Vector声明为public或protected(同包内访问)或写一个get方法返回这个对象即可. 例如:
public class a{
private Vector v;
public Vector getV(){
return v;
}
}
public class a{
private Vector v;
public Vector getV(){
return v;
}
}
|
try:
public class userManager{
private Hashtable hashUsers;
public userManager() {
hashUsers = new Hashtable();
}
public void putUserInfo(String userKey,String userManager) {
if (hashUsers.containsKey(userKey)==null) {
Vector vec = new Vector();
vec.add(userManager);
hashUsers.put(userKey,vec);
} else {
Vector vec = (Vector)hashUsers.get(userKey);
hashUsers.put(userKey,vec);
}
}
public Vector getUserInfo(String userKey) {
return (Vector)hashUsers.get(userKey);
}
}
public class userManager{
private Hashtable hashUsers;
public userManager() {
hashUsers = new Hashtable();
}
public void putUserInfo(String userKey,String userManager) {
if (hashUsers.containsKey(userKey)==null) {
Vector vec = new Vector();
vec.add(userManager);
hashUsers.put(userKey,vec);
} else {
Vector vec = (Vector)hashUsers.get(userKey);
hashUsers.put(userKey,vec);
}
}
public Vector getUserInfo(String userKey) {
return (Vector)hashUsers.get(userKey);
}
}