大家好,我正在尝试使用我的 Fish 类
中的属性,它在我的 Fisher 类
中实现了一个名为 Catchable
的接口(interface),这可能吗,或者是否有我不理解的接口(interface)的某些部分。因为我认为我们被允许在一个由接口(interface)实现的类中使用属性,在另一个类中,但是我一直错误地说:
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 类
实现了 Catchable
接口(interface)
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 类
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;
}
}
最佳答案
问题是以下几行:
保持()
:sumOfWeight += item.weight;
likes()
:if(item.size >= this.keepSize)
giveAwayFish()
:sumOfWeight -= item.weight;
在每种情况下,item
都是 Catchable
类型,而 Catchable
没有 size
或 权重
字段。您需要做的是调用 item.getWeight()
而不是 item.weight
并将 getSize()
方法添加到 Catchable
,并调用 item.getSize()
而不是 item.size
:
可捕获
:public interface Catchable { public float getWeight(); public int getSize(); public boolean isDesirableTo(Fisher f); }
保持()
:sumOfWeight += item.getWeight();
likes()
:if(item.getSize() >= this.keepSize)
giveAwayFish()
:sumOfWeight -= item.getWeight();
您不必修改 Fish
,因为它已经实现了 getSize()
。
而且您真的应该使用像 Eclipse 这样的 IDE,因为它可以实时显示您的错误位置。
关于java - 在实现另一个类接口(interface)的类中使用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962929/