简体   繁体   English

访问由私有自定义类组成的链表的元素

[英]accessing elements of a linked list that is made up of a private custom class

in a project i am trying to get two or more agents to communicate between each other to collect things in the environment. 在一个项目中,我试图让两个或多个代理相互之间进行通信以收集环境中的事物。 to do this i am using a mailbox containing messages that they would respond to depending on the messages sent ebtween each other. 为此,我使用的是一个包含消息的邮箱,它们将根据彼此之间发送的消息进行响应。 below is where i create the linked list 下面是我创建链接列表的地方

mailbox = new LinkedList[numberOfAgents()];
    for ( int i=0; i< numberOfAgents(); i++ ){ 
        mailbox[i] = new LinkedList<Message>();
        mailbox[i].add(new Message(i, knownBloodBanks, knownDonors));
}

and then the private message and intention classes 然后是私人讯息和意图类别

private class Message 
{ 
    // instance variables 
    private int senderId;  
    private BoardComponentList donors; 
    private BoardComponentList bloodBanks; 
    private BoardComponent requestAssistanceAt; 
    private Intention iIntendToAssistAt; 

    public Message( int senderId, BoardComponentList d, BoardComponentList b ) 
    { 
        this.senderId = senderId;  
        donors = d; 
        bloodBanks = b; 
    } // end constructor 

    public void setIntentions( Intention intention ) 
    { 
        iIntendToAssistAt = intention; 
    } 

    public void setRequest( BoardComponent bC ) 
    { 
        requestAssistanceAt = bC; 
    } 

    public BoardComponentList getDonors() 
    { 
        return donors; 
    } 

    public BoardComponentList getBloodBanks() 
    { 
        return bloodBanks; 
    } 

    public Intention getIntentions() 
    { 
        return iIntendToAssistAt; 
    } 

    public BoardComponent getRequest() 
    { 
        return requestAssistanceAt; 
    } 

    public int getSenderId() 
    { 
        return senderId; 
    } 


} // end Message class 

private class Intention
{
    // instance variables 
    private BoardComponent target; 
    private double distanceTo; 

    public Intention( BoardComponent bC, double distance ) 
    { 
        target = bC; 
        distanceTo = distance; 
    } // end constructor 


    public BoardComponent getTarget() 
    { 
        return target; 
    } 

    public double getDistancetoTarget() 
    { 
        return distanceTo;
    } 

}

i cant for the life of me figure out how to access the methods inside the private class so that i can set goals and see the messages between agents. 我无法终生了解如何访问私有类中的方法,以便我可以设定目标并查看代理之间的消息。 any help or pointers in the right direction would be greatly appreciated, i hope i've included enough info if not please let me know anything else needed 任何帮助或朝着正确方向的指示将不胜感激,我希望我提供了足够的信息,如果没有,请告诉我其他需要的信息

i didnt explain myself clearly as i so often find problems with but yes both the private classes and the first code snippet are found inside a public class 我没有清楚地解释自己,因为我经常发现问题,但是是的,私有类和第一个代码片段都在公共类中找到

  1. There can be only one public class per file. 每个文件只能有一个公共类。
  2. The name of the file name(program name) should match with the name of the pubic class. 文件名(程序名)的名称应与公共类的名称匹配。
  3. There can be multiple private classes inside your public class as inner classes. 公共类内部可以有多个私有类作为内部类。

Sample code below ( modified your classes a bit because they we giving compilation error): 下面的示例代码(对类进行了一些修改,因为它们会导致编译错误):

package com.pkg1;

import java.util.LinkedList;

public class Sample{


    public Sample(){
        LinkedList[]    mailbox = new LinkedList[10];
        for ( int i=0; i< 10; i++ ){ 
            mailbox[i] = new LinkedList<Message>();
            mailbox[i].add(new Message(i));
    }
    }
    private class Message 
    { 
        // instance variables 
        private int senderId;  

        private Intention iIntendToAssistAt; 

        public Message( int senderId ) 
        { 
            this.senderId = senderId;  

        } // end constructor 

        public void setIntentions( Intention intention ) 
        { 
            iIntendToAssistAt = intention; 
        } 



        public Intention getIntentions() 
        { 
            return iIntendToAssistAt; 
        } 


        public int getSenderId() 
        { 
            return senderId; 
        } 


    } // end Message class 

    private class Intention
    {
        // instance variables 

        private double distanceTo; 

        public Intention( double distance ) 
        { 
            distanceTo = distance; 
        } // end constructor 


        public double getDistancetoTarget() 
        { 
            return distanceTo;
        } 

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM