我在从构造函数转到不同类中的主方法时丢失数据。发生了什么?

huangapple go评论140阅读模式
英文:

I am losing data when going from constructor to main method in a different class. What is happening?

问题

public class tuna 
{
   private int hour;
   private int minute;
   private int second;
   
   // 0 参数的构造函数
   public tuna()
   {
      this(0,0,0);
   }
   
   // 1 参数的构造函数
   public tuna(int h)
   {
      this(h,0,0);
   }
   
   // 2 参数的构造函数
   public tuna(int h, int m)
   {
      this(h,m,0);
   }
   
   // 3 参数的构造函数
   public tuna(int h, int m, int s)
   {
      setTime(h,m,s);
   }
   
   // 设置时间的方法
   public void setTime(int h, int m, int s)
   {
      setHour(h);
      setMinute(m);
      setSecond(s);
   }
   
   /*----------设置方法----------*/
   public void setHour(int h) {hour = ((h>=0 && h<24) ? h : 0);}
   public void setMinute(int m) {minute = ((m>=0 && m<60) ? m : 0);}
   public void setSecond(int s) {second = ((s>=0 && s<60) ? s : 0);}
   
   /*----------获取方法----------*/
   public int getHour() { 
      return hour;}
   public int getMinute() { 
      return minute;}
   public int getSecond() { 
      return second;}
   
   public String toMilitary()
   {
      return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
   }
}
class apples
{
   public static void main(String[] args)
   {
      tuna obj1 = new tuna(); // 0 参数构造函数
      tuna obj2 = new tuna(5); // 1 参数构造函数
      tuna obj3 = new tuna(5,13); // 2 参数构造函数
      tuna obj4 = new tuna(5,13,43); // 3 参数构造函数
      
      System.out.printf("%s\n", obj1.toMilitary());
      System.out.printf("%s\n", obj2.toMilitary());
      System.out.printf("%s\n", obj3.toMilitary());
      System.out.printf("%s\n", obj4.toMilitary());
   }
}
英文:
```
public class tuna 
{
private int hour;
private int minute;
private int second;
// Constructor with 0 passed arguments
public tuna()
{
this(0,0,0);
}
// Constructor with 1 passed arguments
public tuna(int h)
{
this(h,0,0);
}
// Constructor with 2 passed arguments
public tuna(int h, int m)
{
this(h,m,0);
}
// Constructor with 3 passed arguments
public tuna(int h, int m, int s)
{
setTime(h,m,s);
}
// setTime method
public void setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
/*----------Set methods----------*/
public void setHour(int h) {hour = ( (h&gt;=0 &amp;&amp; h&lt;24)?h : 0);}
public void setMinute(int m) {hour = ( (m&gt;=0 &amp;&amp; m&lt;60)?m : 0);}
public void setSecond(int s) {hour = ( (s&gt;=0 &amp;&amp; s&lt;60)?s : 0);}
/*----------Get methods----------*/
public int getHour() { 
return hour;}
public int getMinute() { 
return minute;}
public int getSecond() { 
return second;}
public String toMilitary()
{
return String.format(&quot;%02d:%02d:%02d&quot;, getHour(), getMinute(), getSecond());
}
}
```

Above is the class containing the constructors and methods and below is the class with the main function that actually calls on the constructors and methods.

I was making this simple program that displays the time and the exercise was showing that you can have multiple constructors with different amounts of parameters. The only problem is when I am at the step of the constructor all the information is correct but when the debugger jumps from the constructor to the class it is being used in all the information is gone except this one line. I think it has something to do with the way I set the constructors up but I can not figure out what I did wrong. Any help would be great. Thank you!

Here is the output that I get:

我在从构造函数转到不同类中的主方法时丢失数据。发生了什么?

The desired output:

我在从构造函数转到不同类中的主方法时丢失数据。发生了什么?

And in case any of you recognize this exercise it is one of Bucky's Tutorials.
P.S. I normally would not post all the code but I literally have no idea what may or may not be pertinent to my problem.


class apples
{
public static void main(String[] args)
{
tuna obj1 = new tuna(); // No parameter constructor
tuna obj2 = new tuna(5); // 1 parameter constructor
tuna obj3 = new tuna(5,13); // 2 parameter constructor
tuna obj4 = new tuna(5,13,43); // 3 parameter constructor
System.out.printf(&quot;%s\n&quot;, obj1.toMilitary());
System.out.printf(&quot;%s\n&quot;, obj2.toMilitary());
System.out.printf(&quot;%s\n&quot;, obj3.toMilitary());
System.out.printf(&quot;%s\n&quot;, obj4.toMilitary());
}
}

答案1

得分: 2

你在所有的设置器中只分配了 hour,但你应该设置 hourminutesecond

/*----------设置方法----------*/
public void setHour(int h) {hour = ((h >= 0 && h < 24) ? h : 0);}
public void setMinute(int m) {minute = ((m >= 0 && m < 60) ? m : 0);}
public void setSecond(int s) {second = ((s >= 0 && s < 60) ? s : 0);}
英文:

You are assigning hour in all setters, while you should be setting hour, minute and second:

/*----------Set methods----------*/
public void setHour(int h) {hour = ( (h&gt;=0 &amp;&amp; h&lt;24)?h : 0);}
public void setMinute(int m) {minute = ( (m&gt;=0 &amp;&amp; m&lt;60)?m : 0);}
public void setSecond(int s) {second = ( (s&gt;=0 &amp;&amp; s&lt;60)?s : 0);}

huangapple
  • 本文由 发表于 2020年5月29日 12:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定