[英]How to create a constructor that takes the capacity of a class that contains an integer and an array of objects
Hello i defined the following Fish class with the following private attributes(basically its just a class that contains a string indicting the type of fish and an integer containing its size in centimeters. I also created get methods for each attribute,a constructor that takes the size and species of the fish and a toString() method that returns a string in the following format: A 10cm Pike ): 您好,我用以下私有属性定义了以下Fish类(基本上,它只是一个包含指示鱼的类型的字符串和一个包含其大小(以厘米为单位)的整数的类。我还为每个属性创建了get方法,该方法使用鱼的大小和种类,以及一个toString()方法,该方法以以下格式返回字符串:10cm Pike):
public class Fish
{
private int size;
private String species;
public int getSize()
{
return this.size;
}
public String getSpecies()
{
return this.species;
}
public Fish(int s, String p)
{
size = s;
species = p;
}
public String toString()
{
return("A " + this.size + " cm " + this.species);
}
}
Then I defined a Pond class that defines the following private attributes: 然后,我定义了一个Pond类,该类定义了以下私有属性:
fish - an array which will contain all Fish objects that are in the pond 鱼-包含池塘中所有鱼对象的数组
numFish - an int indicating the # of fish that are currently in the pond numFish-一个整数,指示当前在池塘中的鱼的数量
and this what I did: (if I am wrong please let me know) 这就是我所做的:(如果我错了,请告诉我)
public class Pond
{
private int numFish;
private Fish [] fishes;
}
now what I am having trouble with in particular is creating a constructor that takes the capacity of the pond, where the capacity is the maximum number of fish that can be stored in the pond at any time. 现在,我特别麻烦的是创建一个构造函数,该构造函数占用了池塘的容量,该容量是随时可以存储在池塘中的最大鱼类数量。 I am not really sure how to create an array as a parameter of a constructor.
我不太确定如何将数组创建为构造函数的参数。 Also I am having trouble with creating a method called isFull() which returns a boolean indicating whether or not the pond has reached its capacity of fish.
同样,我在创建名为isFull()的方法时遇到了麻烦,该方法返回一个布尔值,该值指示池塘是否达到了其捕捞能力。 Any help is appreciated, thank you so much!
感谢您的任何帮助,非常感谢!
So, you want a constructor that takes a capacity as argument: 因此,您需要一个以容量为参数的构造函数:
public Pond(int capacity)
And the capacity is thus the length of the array used to store the fishes: 因此,容量就是用来存储鱼的数组的长度:
public Pond(int capacity) {
this.fishes = new Fish[capacity];
}
Arrays, like almost everything else, are covered in the Java tutorial. 数组和几乎所有其他内容一样,都包含在Java教程中。 Googling for "Java tutorial arrays" finds it in an instant: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
搜寻“ Java教程数组”可立即找到它: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Now, when is the pond full? 现在,池塘什么时候满了? It seems to me that it's full if the number of fishes is equal to its capacity:
在我看来,如果鱼的数量等于其能力,就已经足够了:
public boolean isFull() {
return numFish == fishes.length;
}
You have to make the constructor like that or you can make the setter methods in your Pond class 您必须使像这样的构造函数,或者可以在Pond类中使用setter方法
public Pond(int capacity) {
this.fishes = new Fish[capacity];
}
and you can create isFull method logic like 您可以创建isFull方法逻辑,例如
if (numFish == capacity)
{
System.out.println("Pond is full");
}
Use a constructor similar to the following: 使用类似于以下内容的构造函数:
public Pond(int numberOfFish)
{
numFish=numberOfFish;
fishes=new Fish[numberOfFish];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.