大家好,我正在尝试使用Fish class中的属性,该属性在我的Catchable中实现了称为Fisher class的接口,这是否可能,或者我是否不了解接口的某些部分。因为我以为我们被允许在已经由接口实现的类中使用属​​性,所以在另一个类中使用了属性,但是我一直说错误:

Error: cannot find symbol
  symbol:   variable weight
  location: variable item of type Catchable

Error: cannot find symbol
  symbol:   variable size
  location: variable item of type Catchable

Error: cannot find symbol
  symbol:   variable weight
  location: variable item of type Catchable .


任何帮助或建议对此表示赞赏!

如果需要,这是我的Catchable界面:

public interface Catchable
{
  public float getWeight();
  public boolean isDesirableTo(Fisher f);
}


我的Fish class实现了Catchable接口

public abstract class Fish implements Catchable
{
    // Any fish below this size must be thrown back into the lake
    public static int  THROW_BACK_SIZE = 18;
    public static float WEIGHT_LIMIT = 10;

    protected float weight;
    protected int  size;

    public Fish(int aSize, float aWeight)
    {
        size = aSize;
        weight = aWeight;
    }


    public boolean isDesirableTo(Fisher f)
    {
        if(canKeep() && f.numThingsCaught < f.LIMIT && this.weight + f.sumOfWeight < WEIGHT_LIMIT)
        {
          return true;
        }
        else
        {
        return false;
        }
    }

    public abstract boolean canKeep();

    public int getSize() { return size; }
    public float getWeight() { return weight; }


    public String toString ()
    {
        return ("A " + size + "cm " + weight +  "kg " + this.getClass().getSimpleName());
    }

}


最后是我的Fisher class

import java.util.*;

public class Fisher
{
  private String name;
  private Catchable [] thingCaught;
  public int numThingsCaught;
  private int keepSize;
  public float  sumOfWeight;
  public static int LIMIT = 10;

  public String getName()
  {
    return this.name;
  }

  public int getNumThingsCaught()
  {
    return this.numThingsCaught;
  }

  public int getKeepSize()
  {
    return this.keepSize;
  }


  public Fisher(String n, int k)
  {
    name = n;
    keepSize = k;
  }

  public String toString()
  {
    return(this.name + " with " + this.numThingsCaught + " fish");
  }
  private ArrayList<Catchable> thingsCaught = new ArrayList<Catchable>();


  public void keep(Catchable item)
  {
    if(this.numThingsCaught < LIMIT)
    {
      thingsCaught.add(item);
      numThingsCaught++;
      sumOfWeight += item.weight;
    }
  }

  public boolean likes(Catchable item)
  {
    if(item.size >= this.keepSize)
    {
      return true;
    }

    else
    {
      return false;
    }
  }

  public void listThingsCaught()
  {
    System.out.println(this.toString());

    for(Catchable item : thingsCaught)
    {
      System.out.println(item.toString());
    }
  }

  public void goFishingIn(Lake lake)
  {
    Catchable item = lake.catchSomething();

    if(likes(item))
    {
      this.keep(item);
    }
    else
    {
      lake.add(item);
    }
  }

  public void giveAwayFish(Fisher fisher, Lake lake)
  {
    for(Catchable item : thingsCaught)
    {
      if(fisher.likes(item))
      {
        fisher.keep(item);
      }
      else
      {
        lake.add(item);
      }
      sumOfWeight -= item.weight;
    }
    thingsCaught.clear();
    this.numThingsCaught = 0;

  }

}

最佳答案

问题是以下几行:


keep()

sumOfWeight += item.weight;

likes()

if(item.size >= this.keepSize)

giveAwayFish()

sumOfWeight -= item.weight;



在每种情况下,item都是Catchable类型,并且Catchable没有sizeweight字段。您要做的是调用item.getWeight()而不是item.weight并将getSize()方法添加到Catchable,然后调用item.getSize()而不是item.size


Catchable

public interface Catchable
{
  public float getWeight();
  public int getSize();
  public boolean isDesirableTo(Fisher f);
}

keep()

sumOfWeight += item.getWeight();

likes()

if(item.getSize() >= this.keepSize)

giveAwayFish()

sumOfWeight -= item.getWeight();



您不必修改Fish,因为它已经实现了getSize()
您应该真正使用像Eclipse这样的IDE,因为它可以实时显示错误所在。

10-04 18:11