简体   繁体   English

构造函数无法设置从另一个类获取的数据

[英]constructor can't set data taken from another class

Long story I can't figure out how to load the jet data jetid and currentLocation into j . jetid ,我不知道如何将jet数据jetidcurrentLocation加载到j Any help is appreciated. 任何帮助表示赞赏。 I apologize before hand if I referred to things wrongly. 如果我错误地提到事情,我事先表示歉意。 Thank you. 谢谢。

public class Corporation {

    public Jet[] jets = new Jet[14];



    public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
    {
        //create 14 new jets


        for(int i =0; i < jets.length; i++)
        {
            //make a new jet here
            Jet j = new Jet();
            j.jetid = 
        }

    }

}

The second class the one I'm trying to pull jetid and currentLocation from is: 我正在尝试从中提取jetidcurrentLocation的第二类是:

public class Jet {

    public int jetid;
    public Delivery deliveryArray[];
    public String currentLocation; //where it's currently sitting 

    public Jet()
    {
        int random = (int)(Math.random()*10000);

        this.jetid = random;
        this.currentLocation = setCurrentLocation();
        System.out.println("NEW JET IS ONLINE WITH JET ID: " + this.jetid + " AT LOCATION " + this.currentLocation);

    }

    private String setCurrentLocation() 
    {
        //set up a random # that determines which city the plane is based in
        double random = (Math.random());
        String location = " ";

        if(random < .1 && random > 0)
            location = "PHX";

        else if(random > .1 && random < .2)
            location = "SAN";

        else if(random > .2 && random <.3)
            location = "LAX";

        else if(random > .3 && random < .4)
            location = "POR";

        else if(random > .4 && random < .5)
            location = "SEA";

        else if(random > .5 && random <.6)
            location = "SAF";
        else
            location = "DAL";


        return location;
    }

}

Thanks everyone for the input. 感谢大家的投入。 Other problems aside I settled on this code shortly after posting this question: 除了其他问题外,我在发布此问题后不久就解决了此代码:

Jet j = new Jet();
jets[i]=j;

And from there I've been able to figure out all other issues. 从那里,我已经能够找出所有其他问题。 Thanks again everyone for your input 再次感谢大家的投入

Not sure what error you are getting, I assume, your constructor: 我不确定您所构造的错误是什么:

public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
{
    //create 14 new jets


    for(int i =0; i < jets.length; i++)
    {
        //make a new jet here
        Jet j = new Jet();
        j.jetid = 
    }

}

Should be the following, if you declared a local j[] in your Corporation class 如果您在Corporation类中声明了本地j[] ,则应为以下内容

public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
{
    //create 14 new jets


    for(int i =0; i < jets.length; i++)
    {
        //make a new jet here
        Jet j[i] = new Jet();
        //j.jetid = no need to set this
    }

}

The other thing wrong is the j.jetid = . 另一件事是j.jetid = But you do not need to set it, as it is already set in the Jet constructor to this.jetid = random; 但是您不需要设置它,因为它已经在Jet构造函数中设置为this.jetid = random; . Happens every time you instantiate a new Jet in Jet j[i] = new Jet(); 每次在Jet j[i] = new Jet();实例化一个新Jet时都会发生Jet j[i] = new Jet();

The code appears to create 14 jets, all of which initialise their own data (eg they already have jetId and current location). 该代码似乎创建了14个喷射器,所有这些喷射器均初始化了自己的数据(例如,它们已经具有jetId和当前位置)。 The data's already there, the Corporation doesn't need to set it. 数据已经存在,公司无需进行设置。

If you're saying you want the corp to create it, then create a new constructor, Jet(int jetId, String currentLocation) and you can pass the data in. 如果您要让公司创建它,则创建一个新的构造函数Jet(int jetId,String currentLocation),然后可以传入数据。

The corporation object will have an array with 14 references to the Jet object. 公司对象将具有一个数组,其中包含对Jet对象的14个引用。

public class Corporation {

  private Jet[] jets = new Jet[14];

  // Constructor
  // RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
  public Corporation() { 

    //create 14 new jets
    for (int i =0; i < jets.length; i++)
    {
        //make a new jet here
        Jet j = new Jet();
        //save the reference to array
        jets[i] = j;
        // more Jet intialization
        j.id = ...
    }
  }

  // accessor
  public Jet [] getJets() {
    ...
  }

} 

You probably want to pass things in through the constructor: 您可能想通过构造函数传递东西:

public Jet(int jetid, String currentLocation)
{
    this.jetid = jetid;
    this.currentLocation = currentLocation;
}

Now you can write the following in your corporation class: 现在,您可以在公司类中编写以下内容:

    for(int i =0; i < jets.length; i++)
    {
        //make a new jet here
        int randomId = (int)(Math.random()*10000);
        String randomLocation = randomLocation();
        jets[i] = new Jet(randomId, randomLocation);
    }

That randomLocation is just your old setCurrentLocation method, from your Jet class. 那个randomLocation只是您的Jet类中的旧setCurrentLocation方法。 You should rename it and move it into your Corporation class, since Corporation is now responsible for making up random locations. 您应该重命名它,并将其移入Corporation类,因为Corporation现在负责组成随机位置。 Or you could rename it and leave it in Jet, and make it public static, and you could implement it in just 2 lines: 或者,您可以重命名它并将其保留在Jet中,并使它成为公共静态对象,并且可以仅用两行实现它:

public static String randomLocation() {
    // "DAL" is twice as likely as the others:
    String[] array = {"ABC", "DEF", "DAL", "DAL"};
    return array[new Random().nextInt(array.length)];
}

and then invoke it as follows: 然后按以下方式调用它:

String randomLocation = Jet.randomLocation();

您需要将变量传递给jet类的构造函数!

Jet jet = new Jet(string _jetID, string _currentLocation)

If i have understood your question correctly, you want to set jetid and currentLocation , than you need to change your Jet constructor like the following: 如果我正确理解了您的问题,则需要设置jetidcurrentLocation ,而不是像下面那样更改Jet构造函数:

public Jet(int jetid, String currentLocation)
{ 
this.jetid = jetid;
this.currentLocation = currentLocation;
}

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

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