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

时间:2015-03-15 03:38:52

标签: java constructor instance

长篇故事我无法弄清楚如何将喷气数据jetidcurrentLocation加载到j。任何帮助表示赞赏。如果我错误地提到了事情,我会事先道歉。谢谢。

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 = 
        }

    }

}

我尝试从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;
    }

}

7 个答案:

答案 0 :(得分:1)

感谢大家的投入。除了其他问题,我在发布此问题后不久就确定了这段代码:

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

从那时起,我就能找出所有其他问题。再次感谢大家的意见

答案 1 :(得分:0)

我不知道你的构造函数是什么错误:

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 = 
    }

}

如果您在公司类中声明了本地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
    }

}

另一个错误是j.jetid =。但是您不需要设置它,因为它已经在Jet构造函数中设置为this.jetid = random;。每次在Jet

中实例化新的Jet j[i] = new Jet();时都会发生错误

答案 2 :(得分:0)

代码似乎创建了14个喷嘴,所有这些喷嘴都初始化了他们自己的数据(例如,他们已经拥有jetId和当前位置)。数据已存在,公司不需要设置它。

如果你想要公司创建它,那么创建一个新的构造函数Jet(int jetId,String currentLocation),你可以传入数据。

答案 3 :(得分:0)

公司对象将有一个包含14个Jet对象引用的数组。

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() {
    ...
  }

} 

答案 4 :(得分:0)

您可能希望通过构造函数传递内容:

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

现在您可以在公司类中编写以下内容:

    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);
    }

{J}只是您的旧randomLocation方法,来自您的Jet类。您应该重命名它并将其移动到您的公司类中,因为公司现在负责组成随机位置。或者您可以将其重命名并将其保留在Jet中,并使其成为公共静态,并且您只需2行即可实现它:

setCurrentLocation

然后按如下方式调用它:

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)];
}

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

如果我已正确理解您的问题,您需要设置jetidcurrentLocation,而不是需要更改Jet构造函数,如下所示:

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