对于以下Ocean课程,
public class Ocean {
/**
* Define any variables associated with an Ocean object here. These
* variables MUST be private.
*/
// width of an Ocean
private final int width;
// height of an Ocean
private final int height;
class Critter {
/**
* Defines a location of a Critter in an Ocean.
*/
Point location;
public Critter(int x, int y) {
location = new Point(x,y);
}
public Point getLocation() {
return location;
}
}
private Critter[][] oceanMatrix;
}
Run Code Online (Sandbox Code Playgroud)
我想Critter从下面的类Shark构造函数访问上面的类中的构造函数.
class Shark extends Ocean implements Behaviour {
public Shark(int x, int y, int hungerLevel) {
super(x,y);
}
}
Run Code Online (Sandbox Code Playgroud)
如何Critter从Shark类构造函数中访问类构造函数?
看起来你应该延伸Critter而不是海洋:
class Shark extends Ocean.Critter implements Behaviour{
...
public Shark(int x, int y, int hungerLevel){
super(x,y);
}
...
}
Run Code Online (Sandbox Code Playgroud)
为了实现这一点,Critter需要成为一个静态的内部类.我不知道这个设计有多少是你的,但是内部类应该限于彼此强烈依赖的类,这不是这里的情况.如果可以的话,把Critter带出海洋.
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |