2011-06-15 73 views
7

我怎樣才能實現這個功能呢?如何訪問一個Bean的存儲值從另一個類中

ApplicationConstants.phoneContacts.add(
    new ContactNumberBean(nameOfContact, 
    contactNumber, contactNumberType)); 

ApplicationConstants和ContactNumberBean類

ContactNumberBean:

package com.example.AddressBook; 

public class ContactNumberBean 
{ 
    private String nameOfContact; 
    private String contactNumber; 
    private int contactNumberType; 

    public String getnameOfContact() 
    { 
     return nameOfContact; 
    } 
    public String getcontactNumber() 
    { 
     return contactNumber; 
    } 
    public int getcontactNumberType() 
    { 
     return contactNumberType; 
    } 
    public ContactNumberBean(String nameOfContact, String contactNumber,int contactNumberType) 
    { 
     this.nameOfContact=nameOfContact; 
     this.contactNumber=contactNumber; 
     this.contactNumberType=contactNumberType; 

    } 
} 

ApplicationConstants:

package com.example.AddressBook; 

import java.util.ArrayList; 

public class ApplicationConstants 
{ 
    //String[] phoneContacts =new String[10]; 
    //ArrayList<NameValuePair> list = new ArrayList<NameValuePair>(); 

    /*String s1,s2; 
    int i1; 

    ContactNumberBean cb =new ContactNumberBean(str1,str2,i2); 

    static ArrayList<String> phoneContacts = new ArrayList<String>(); 
    phoneContacts.add(s1); 
    phoneContacts.add(s2); 
    phoneContacts.add(i1);*/ 

how can implemented in this class functionality ................................ 

**ApplicationConstants.phoneContacts.add(new ContactNumberBean(nameOfContact, 
             contactNumber, contactNumberType));** 


} 

請一些解決方案在此先感謝轉發....納拉辛哈

+0

能否uppervoters的人編輯呢?第一段不太清楚。這是一種英語嗎? – ernazm 2011-06-15 09:01:21

回答

1

喜來訪問另一個類是價值觀創建命名爲Constants.java在聲明一個公共類和初始化對象已經象下面這樣:

public class Constants{ 

    public static Bean userBeen=new Bean(); 

} 

被類:

public class Been { 

    private string countryName; 

    public void setCountry(String s) { 
     this.countryName=s; 
    } 

    public String getCountry() { 
     return countryName; 
    } 
} 

設定值:

public class A{ 

    String s="India"; 

    Constants.userBeen.setCountry(s); 

    } 
} 

獲取值:

public class B{ 

    String s=Constants.userBeen.getCountry(); 

    } 

} 

這將工作正常。

相關問題