Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Person类的编写方法_Java_Object_Methods_Boolean_Drjava - Fatal编程技术网

Java Person类的编写方法

Java Person类的编写方法,java,object,methods,boolean,drjava,Java,Object,Methods,Boolean,Drjava,我正在尝试编写一个构造函数和一个满足以下输出的方法,但在开始时遇到了困难 4.9 20.0 0 false 4.9' person with $20.00 and 0 tickets 4.9' person with $20.00 and 3 tickets 4.9' person with $20.00 and 1 tickets 4.9' person with $20.00 and a pass 这是测试代码: public class Person2Tester { pub

我正在尝试编写一个构造函数和一个满足以下输出的方法,但在开始时遇到了困难

4.9
20.0
0
false
4.9' person with $20.00 and 0 tickets
4.9' person with $20.00 and 3 tickets
4.9' person with $20.00 and 1 tickets
4.9' person with $20.00 and a pass
这是测试代码:

public class Person2Tester 
{ 
    public static void main(String args[]) 
    { 
        Person     mary; 

        mary = new Person(4.9f, 20.00f); 

        System.out.println(mary.height); 
        System.out.println(mary.money); 
        System.out.println(mary.ticketCount); 
        System.out.println(mary.hasPass); 
        System.out.println(mary);   // Notice the money is properly formatted

        mary.ticketCount = 3; 
        System.out.println(mary);

        mary.useTickets(2);   // You have to write this method
        System.out.println(mary);

        mary.hasPass = true; 
        System.out.println(mary); 
    } 
}
这是我到目前为止的代码:

public class Person
{
  float height;
  float money;
  int ticketCount;
  boolean hasPass;

  public Person()//empty constructor
  {
    height = 0.0f;
    money = 0.0f;
    ticketCount = 0;
    hasPass = false;
  }

  public Person(float h, float m) 
  {
    height = h;
    money = m;
    ticketCount = 0;
    hasPass = false;

  }
  public String toString() 
  {

    return(this.height + " person with " + this.money + " and " + this.ticketCount + " tickets");
  }

}

这是我完成的代码。感谢所有帮助过我的人

  public String toString() 
  {
    if(hasPass)
    {
    return(this.height + "' person with $" + this.money + " and has a pass");
    }
    else
    {
      return(this.height + "' person with $" + this.money + " and " + this.ticketCount + " tickets");
    }
  }
  public void useTickets(int numTickets)
  {
    if(this.ticketCount >= numTickets)
    {
      this.ticketCount -= numTickets;
    }
  }

考虑需要向
Person
类传递什么样的信息来创建
Person
对象。
Person
的构造函数不带参数,但您试图在测试代码中传递两个参数,这有意义吗


您还需要为
Person
类编写一些其他函数,但是如果您考虑一下我上面写的内容,您应该可以开始了。

创建setter和getter并传递值将是一种更好的方法

public class Person2Tester{  

     public static void main(String args[]) 
{ 
    Person mary = new Person(); 
    Person person2 = new Person();
   // add as many as you want perosn3 ,4 ..

    mary.setHeight(1);
    mary.setMoney(200);
    mary.setHasPass(false);
    mary.setTicketCount(4);



    System.out.println(mary.getHeight()); 
    System.out.println(mary.getMoney()); 
    System.out.println(mary.ticketCount); 
    System.out.println(mary.isHasPass()); 
    System.out.println(mary);   // Notice the money is properly formatted

     //add your methods here
} 
}



 public class Person{

  float height;
  float money;
  int ticketCount;
  boolean hasPass;

  public Person( )
{
     this.height = height;
     this.money = money;
     this.ticketCount = ticketCount;
     this.hasPass = hasPass;
}

public float getHeight() {
    return height;
}

public void setHeight(float height) {
    this.height = height;
}

public float getMoney() {
    return money;
}

你的构造函数没有参数,在测试中你传递了2个参数。提示:不要在
Person
类中重复声明变量我的老师说我们应该始终将构造函数设置为零参数@hugosusa你的老师可能应该开始编织了。你定义了一个没有参数的构造函数,是的。但是你在测试中用两个参数调用它。您没有声明任何具有2个参数的构造函数,因此不应该编译。有人告诉我,我应该始终放置一个具有零参数seh的构造函数,但并不总是,但您永远不应该在构造函数中声明变量。请确保使用点运算符显式设置变量的值:
mary.height=4.9
,或者确保构造函数接受参数,以便执行以下操作:
Person mary=new Person(4.9,…)
如果使用getter和setter,适用的成员变量通常也应该是
private
,并且测试代码不应该被更改,因此这不会有很大帮助,可能也不应该是答案。@Dukeling感谢您的输入。。那么您认为通过构造函数传递值比使用setter和getter有优势吗?你能解释一下吗:)我想他可以创建person类person2=newperson()的另一个实例;“那么你认为通过构造函数传递值比使用setter和getter有优势吗?”-我这样认为(通过构造函数传递通常代码要少得多,而且事情就是这样做的,尽管可读性稍差),但我不确定你是如何从我的评论中得到这一点的。我没有评论getter和setter,只是说您的成员变量是私有的,不是
私有的
,但这实际上是相当小的。主要问题是您对测试代码进行了重大更改,这导致了一个没有那么有用的答案。