为什么Java将我的第二个实例的参数分配给了该类的两个实例?

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

Why is Java assigning the parameters of my second instance to both instances of a class?

问题

这是我的Conveyor类:

public class Conveyor
{
   //fields
   private static String type;
   private static double speed; //速度以m/s(米/秒)为单位衡量
   
   //constructors
   public Conveyor(String type, double speed)
   {
      this.type = type;
      this.speed = 0;
      this.speed = setSpeed(speed);
   }
   
   //methods
   public String getType()
   {
      return this.type;
   }
   
   public double getSpeed()
   {
      return this.speed;
   }
   
   public double setSpeed(double speed)
   {  
      if(speed <= 0)
      {
         this.speed = this.speed;
      }
      else
      {
         this.speed = speed;
      }
      
      return this.speed;
   }
   
   public double timeToTransport(double distance)
   {
      double t = distance / getSpeed();
      return t;
   }
}

以及用于测试的Conveyor App:

public class ConveyorApp
{
   public static void main(String[] args)
   {
      Conveyor c1 = new Conveyor("flat belt",0.9);
      Conveyor c2 = new Conveyor("roller", -0.5);
      
      System.out.printf("Conveyor 1: %s conveyor with a speed of %.1f\n", c1.getType(), c1.getSpeed());
      System.out.printf("Time to transport an item 50m: %.1f\n\n", c1.timeToTransport(50));
      
      System.out.printf("Conveyor 2: %s conveyor with a speed of %.1f\n", c2.getType(), c2.getSpeed());
      System.out.printf("Time to transport an item 50m: %.1f\n\n", c2.timeToTransport(50));
   }
}

如果我将第二个实例移到第一个实例的打印语句下面,它会产生预期的行为并打印出:

Conveyor 1: flat belt conveyor with a speed of 0.9
Time to transport an item 50m: 55.6

Conveyor 2: roller conveyor with a speed of 0.0
Time to transport an item 50m: Infinity

否则,按照上述顺序,输出将会是:

Conveyor 1: roller conveyor with a speed of 0.0
Time to transport an item 50m: Infinity

Conveyor 2: roller conveyor with a speed of 0.0
Time to transport an item 50m: Infinity

我做错了什么?

英文:

I am in an entry level college programming class and this is perhaps a novice question, but I haven't been able to find the answer elsewhere and can't figure out why Java is assigning the parameters from my second instance of Conveyor to both instances.

Here is my Conveyor class:

    public class Conveyor
    {
       //fields
       private static String type;
       private static double speed; //Speed is measured in m/s (meters/second)
       
       //constructors
       public Conveyor(String type, double speed)
       {
          this.type = type;
          this.speed = 0;
          this.speed = setSpeed(speed);
       }
       
       //methods
       public String getType()
       {
          return this.type;
       }
       
       public double getSpeed()
       {
          return this.speed;
       }
       
       public double setSpeed(double speed)
       {  
          if(speed &lt;= 0)
          {
             this.speed = this.speed;
          }
          else
          {
             this.speed = speed;
          }
          
          return this.speed;
       }
       
       public double timeToTransport(double distance)
       {
          double t = distance / getSpeed();
          return t;
       }
    }

And the Conveyor App being used to test it:

    public class ConveyorApp
    {
       public static void main(String[] args)
       {
          Conveyor c1 = new Conveyor(&quot;flat belt&quot;,0.9);
          Conveyor c2 = new Conveyor(&quot;roller&quot;, -0.5);
          
          System.out.printf(&quot;Conveyor 1: %s conveyor with a speed of %.1f\n&quot;, c1.getType(), c1.getSpeed());
          System.out.printf(&quot;Time to transport an item 50m: %.1f\n\n&quot;, c1.timeToTransport(50));
          
          System.out.printf(&quot;Conveyor 2: %s conveyor with a speed of %.1f\n&quot;, c2.getType(), c2.getSpeed());
          System.out.printf(&quot;Time to transport an item 50m: %.1f\n\n&quot;, c2.timeToTransport(50));
       }
    }

If I move the second instance below the print statements of the first it produces the expected behavior and prints:

    Conveyor 1: flat belt conveyor with a speed of 0.9
    Time to transport an item 50m: 55.6
    
    Conveyor 2: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity

Otherwise in the order presented above this is my output:

    Conveyor 1: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity
    
    Conveyor 2: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity

What am I doing wrong?

答案1

得分: 2

你的Conveyor类的字段被标记为静态字段,但听起来你希望它们表现得像实例字段一样。

public class Conveyor {
    //字段
    private String type;
    private double speed; //速度以米/秒(m/s,米每秒)为单位衡量
    ...
}

只需像这样移除static关键字,这就是你需要做的全部!

英文:

The fields for your Conveyor class are marked as static fields, but it sounds like you want them to behave as instance fields.

  public class Conveyor
    {
       //fields
       private String type;
       private double speed; //Speed is measured in m/s (meters/second)
       ...

Just removing the static keyword like this should be all you need to do!

huangapple
  • 本文由 发表于 2020年10月6日 12:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219535.html
匿名

发表评论

匿名网友

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

确定