大家好,
所以我创建了一个类Temperature,它有一个构造函数来设置温度。温度是由冷、热两个数字组成的数组列表。
public int hotness;
public int coldness;
public int[] temperature;
public int maxTemperature = 10000;
//Constructor Temperature
public Temperature(int hotness, int coldness) {
/**
* A constructor for the Array Temperature
*/
maxTemperature = getMaxTemperature();
if(hotness <= maxTemperature && coldness <= maxTemperature)
temperature[0] = coldness;
temperature[1] = hotness;
}现在我想进入另一个类,用那个物体温度来做一些计算。这是它的代码。
//Variabels
public int volatility;
private static Temperature temperature;
private static int intrensicExplosivity;
public static int standardVolatility(){
if(temperature[0] == 0){
int standardVolatility = 100 * intrensicExplosivity * (0.10 * temperature[1]);
}所以现在我得到了错误:表达式的类型必须是数组类型,但它被解析为"Temperature“
有什么解决方案吗?
我对Java非常陌生,所以可能只是一些synthax错误,但我就是找不到它。
提前谢谢。大卫
发布于 2012-05-10 15:39:34
而不是
public static int standardVolatility() {
if(temperature[0] == 0) {试一试
public static int standardVolatility() {
if(tepmerature.temperature[0] == 0) {
^^^^^^^^^^^^请注意,第二个代码片段中的temperature是Temperature类型,该类型本身有一个名为temperature的整型数组。要访问Temperature对象的temperature-array,您必须执行temperature.temperature。
正如@Marko Topolnik指出的那样,你可能也想要改变
public int[] temperature;至
public int[] temperature = new int[2];以便为两个温度值腾出空间。
发布于 2012-05-10 15:44:38
首先在温度类中创建getter和setter方法,然后调用temperature.getTempertature()并在第二个类上使用它。
发布于 2012-05-10 15:39:52
tempeture的类型为Tempeture,该类型不是数组。您需要的是对象实例(也称为tempature)中的数组成员temperature。
无论如何,更改行:
if(temperature[0] == 0)
.
.通过以下方式:
if(temperature.tempature[0] == 0)
.
.我建议您使用getter和setter,也可以使用name,这不会让您感到困惑。
https://stackoverflow.com/questions/10529399
复制相似问题