嗨,我在OOP上不是很好,很抱歉,如果有人问过同样的问题。现在我在从另一个对象中获取对象的属性时遇到问题,其中两个对象都属于一个对象
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
}
我想要做的是制作一个包含(MAPID,auctioneer.configuration,profit)的MAP。我的问题是如何从Auctioneer.configuration
类访问/获取DailyAssessmentReport
的值?
我非常感谢你的回答
答案 0 :(得分: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];
我希望这会对你有所帮助。如果没有,例如因为我误解了你的问题,请提供反馈意见,以便我们为您找到合适的解决方案。