简体   繁体   English

我的班级有问题,不太确定我是否正确编写了代码

[英]I am having trouble with my Class, not quite sure if I am writing the code correctly

I am writing a class for a project regarding the Titanic.我正在为有关泰坦尼克号的项目编写课程。 The instructions say Passenger: Represents a passenger of the Titanic, with attributes (instance variables): Instance data: status (an integer: 1, 2, 3, or 4, representing 1st, 2nd, 3rd class or crew child (a boolean: true = child, false = adult) sex (a String: “male” or “female”) survivor (a boolean: true/false indicating whether this passenger survived)指令说乘客:代表泰坦尼克号的乘客,有属性(实例变量):实例数据:状态(整数:1、2、3或4,代表一等、二、三等舱或船员孩子(布尔值: true = 儿童,false = 成人)性别(字符串:“男性”或“女性”)幸存者(布尔值:true/false 表示该乘客是否幸存)

Here is my code currently, not sure if it is off or if I am putting things in the wrong place这是我目前的代码,不确定它是关闭还是我把东西放在错误的地方

//********************************************************************
//  Passenger.java       Author:
//
//  Represents passenger on Titanic.
//********************************************************************

import java.text.NumberFormat;

public class Passenger
{
   int status;
   boolean child;
   String sex;
   boolean survivor;

  //-----------------------------------------------------------------
  //  Creates a new DVD with the specified information.
  //---------------------------------------------------------------- 
  public Passenger (int 1, int 2,int 3, int 4, boolean true, boolean          false, String m, String f)
  {
      1=1stclass;
      2=2ndclass;
      3=3rdclass;
      4=crew;
      true=child;
      false=adult;
      m=male;
      f=female;
  }

}

The most common way to do this would be the following:最常见的方法如下:

public class Passenger {
    int status;
    boolean child;
    String sex;
    boolean survivor;

    public Passenger (int status, boolean child, String sex, boolean survivor) {
        this.status = status;
        this.child = child;
        this.sex = sex;
        this.survivor = survivor;
    }
}

but since you probably do not know what this means, let us just come up with new variable names instead:但由于您可能不知道this意味着什么,让我们改为想出新的变量名称:

public class Passenger {
    int status;
    boolean child;
    String sex;
    boolean survivor;

    public Passenger (int c_status, boolean c_child, String c_sex, boolean c_survivor) {
        status = c_status;
        child = c_child;
        sex = c_sex;
        survivor = c_survivor;
    }
}

A constructor is called when an instance of this class is created by for example例如,当创建此类的实例时调用构造函数

new Passenger(2, true, "female", false)

after which the constructor will take these four values, and do what it is supposed to do.之后构造函数将采用这四个值,并做它应该做的事情。 In our case, it will take these values and assign them to the four fields status , child , sex , and survivor .在我们的例子中,它将获取这些值并将它们分配给四个字段statuschildsexsurvivor

To be able to tell the constructor to do this, we give these parameters a name (so we can refer to them) - in our example: c_status , etc. Then we tell the constructor to take the value of c_status and put it into status where it is saved until the object gets destroyed.为了能够告诉构造函数这样做,我们给这些参数一个名称(以便我们可以引用它们) - 在我们的示例中: c_status等。然后我们告诉构造函数获取c_status的值并将其放入status在对象被销毁之前保存它的位置。

I suggest you open a Java-book and read through it.我建议您打开一本 Java 书并通读一遍。 I am writing this answer mostly to give you a quick-fix.我写这个答案主要是为了给你一个快速解决方案。

Your constructor is incorrect你的构造函数不正确

for constructor parameters you can't use actual values, you must use names for them.对于构造函数参数,您不能使用实际值,您必须为它们使用名称。 It must be:肯定是:

`public Passenger( int status, boolean child, String sex, boolean survivor){
   this.status = status;
   this.child = child;
   this.sex = sex;
   this.survivor = survivor;
}
//you also can add setters and getters for your class attributes
public void setStatus(int status) {
   this.status = status;
}
public int getStatus() {
   return status;
}
//so you can create another setters and getters for all your attributes`

Now try to replace your constructor with this one.现在尝试用这个替换你的构造函数。

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

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