For the below Ocean
class,
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;
}
I would like to access constructor Critter
in above class from below class Shark
constructor.
class Shark extends Ocean implements Behaviour {
public Shark(int x, int y, int hungerLevel) {
super(x,y);
}
}
How do I access Critter
class constructor from Shark
class constructor?
It seems like you should extend Critter
instead of Ocean:
class Shark extends Ocean.Critter implements Behaviour{
...
public Shark(int x, int y, int hungerLevel){
super(x,y);
}
...
}
For this to work, Critter needs to be a static inner class. I don't know how much of this design is yours, but inner classes should be limited to classes that are strongly dependent on each other, this isn't the case here. If you can, take Critter out of Ocean.
只要Shark
与Ocean
在同一包中,并且将static
修饰符添加到Critter
的类声明中,您就应该能够使用new Ocean.Critter()
访问Critter
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.