嗨,大家我試圖使用我的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;
}
}
出於好奇,你有C#背景嗎? –