2012-11-26 161 views
0

嗨我不是很好的OOP,對不起,如果有人問過同樣的問題。現在我從另一個對象,其中兩個對象屬於一個對象acccesing對象的屬性有問題屬於同一類的另一個對象的anobject的屬性

public abstract class GameClient(){ 
protected ClientRegistry registry; 
.. 
} 


public class MarketClient extends GameClient { 
public Auctioneer auctioneer = null; 
public Specialist specialist; 
... 
((GenericDoubleAuctioneer) auctioneer).setRegistry((MarketRegistry) registry); 
specialist = registry.addSpecialist(clientId); 
} 


public class Specialist extends AccountHolder() { 
public Specialist(final String id) { 
     this(id, null); 
... 
} 


public interface Auctioneer extends QuoteProvider(){ 
public MarketRegistry getRegistry(); 
public List configuration 
... } 

    public class DailyAssessmentReport(){ 
protected void calculateProfits() { 
final Specialist specialists[] = GameController.getInstance().getRegistry().getSpecialists(); 
//later, I'll get the ID of each specialist from specialists[]; 
... 
... 
public Map< specialistID, List, Score> Result; 
//this Map contains specialistID , auctioneer.configuration, score 
} 

我想要做的就是讓其中包括一個MAP(specialistID,auctioneer.configuration,利潤) 。我的問題是我怎樣才能從DailyAssessmentReport類訪問/獲得Auctioneer.configuration的值?

我真的很感激你的答案

+0

請明確你的意思是「[...]從另一個對象獲取對象的屬性,其中兩個對象都屬於一個對象」。 –

+0

我有一個'MarketClient'對象,這個'MarketClient'具有屬性''專家'和'拍賣者'(都是不同的類),不知何故我可以從另一個類獲得'專家'對象,我怎樣才能訪問其中的一個當我唯一擁有的是'專家'時,'拍賣者'屬性? – 4reen

回答

0

可以使用地圖,例如爲你的目的一個HashMap。請注意,映射從一個對象映射到另一個對象。他們不會將一個對象映射到另外兩個對象,如您在示例中嘗試執行的操作。但是,您仍然可以通過將兩個目標對象存儲在公用容器對象(例如ArrayList或2元素數組)中,然後映射到容器對象來實現目標。例如:

HashMap<String, Object[]> specialistIdToContainerObjectMap = new HashMap<String, Object[]>(); 

// get two objects which should be looked up later through the map, based on specialist ID 
Specialist specialist = ...; 
Auctioneer auctioneer = ...; 

// create container object to hold the two objects to which we want to map 
Object[] containerObject = new Object[2]; 
containerObject[0] = specialist; 
containerObject[1] = auctioneer; 

// store the container object in the map 
specialistIdToContainerObjectMap.put(specialist.getId(), containerObject); 

你可以再後來查找的容器對象,並從中提取了兩個被引用對象:

// get a specialist id from somewhere, e.g. by doing someSpecialist.getId() 
String specialistId = ...; 

// look up the container object from the map 
Object[] containerObject = specialistIdToContainerObjectMap.get(specialistId); 

// extract the specialist and the auctioneer from the container object 
Specialist specialist = (Specialist) containerObject[0]; 
Auctioneer auctioneer = (Auctioneer) containerObject[1]; 

我希望這可以幫助你。如果不是,例如因爲我誤解了你的問題,請提供反饋意見,以便我們可以嘗試爲你找到合適的解決方案。

相關問題