【发布时间】:2013-08-21 02:32:42
【问题描述】:
这是我的第一个 Java 项目。
所以我正在开发自己的模拟项目,但我的一些核心内容出现了问题。我现在专注于两个课程 - 定居点和 townRey,它扩展了定居点。
当我尝试时抛出错误
System.out.println(townRey.returnStrength());
这是我的两个相关课程:
结算:
public class settlement
{
//
//
// VARIABLES
//
//
/**
* The town's unique name.
*/
public String name;
/**
* The settlement's location in latitude (N-S)
*/
public int latitude;
/**
* The settlement's location in longitude (E-W)
*/
public int longitude;
/**
* What faction a town or village is aligned to. This determines production and consumption, mostly.
*/
public String faction;
/**
* What a specific village or town produces.
*/
public String[] production;
/**
* What a specific town consumes (villages don't consume)
*/
public String[] consumption;
/**
* How dangerous a specific town is with bandits
* A 1-10 scale, with 10 being the most dangerous.
* Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
* Being raided successfully depends on the Strength of a town.
*/
public int danger;
/**
* How much a town takes in taxes.
*/
public float tax;
/**
* How easily a town is raided by bandits.
* If a bandit raid has a lower strength than the town, then the town wins.
*/
public int strength;
//
//
// METHODS
//
//
public int returnLatitude()
{
return latitude;
}
public int returnLongitude()
{
return longitude;
}
public String returnFaction()
{
return faction;
}
public String[] returnProduction()
{
return production;
}
public String[] returnConsumption()
{
return consumption;
}
public int returnDanger()
{
return danger;
}
public float returnTax()
{
return tax;
}
public int returnStrength()
{
return strength;
}
}
和townRey:
public class townRey extends settlement
{{
name = "Rey";
latitude = 5;
longitude = 5;
String faction = "Nord";
String[] production;
String[] consumption;
danger = 1;
tax = 0.05F;
strength = 6;
}}
编辑:: 感谢所有帮助!我现在解决了所有问题。下面是“结算”和“开始”。
public class Settlement
{
//
//
// VARIABLES
//
//
/**
* The town's unique name.
*/
public String name;
/**
* The settlement's location in latitude (N-S)
*/
public int latitude;
/**
* The settlement's location in longitude (E-W)
*/
public int longitude;
/**
* What faction a town or village is aligned to. This determines production and consumption, mostly.
*/
public String faction;
/**
* What a specific village or town produces.
*/
public String[] production;
/**
* What a specific town consumes (villages don't consume)
*/
public String[] consumption;
/**
* How dangerous a specific town is with bandits
* A 1-10 scale, with 10 being the most dangerous.
* Any town with a danger over 8 can be raided and destroyed temporarily by bandits.
* Being raided successfully depends on the Strength of a town.
*/
public int danger;
/**
* How much a town takes in taxes.
*/
public float tax;
/**
* How easily a town is raided by bandits.
* If a bandit raid has a lower strength than the town, then the town wins.
*/
public int strength;
//
//
// METHODS
//
//
public int returnLatitude()
{
return latitude;
}
public int returnLongitude()
{
return longitude;
}
public String returnFaction()
{
return faction;
}
public String[] returnProduction()
{
return production;
}
public String[] returnConsumption()
{
return consumption;
}
public int returnDanger()
{
return danger;
}
public float returnTax()
{
return tax;
}
public int returnStrength()
{
return strength;
}
}
然后开始,我在这里创建“townRey”,然后以两种不同的方式访问一些数据。
public class Start
{
public static void main(String[] args)
{
//Creates 'Rey'
Settlement townRey = new Settlement();
townRey.name = "Rey";
townRey.latitude = 5;
townRey.longitude = 5;
townRey.faction = "Nord";
townRey.danger = 1;
townRey.tax = 0.05F;
townRey.strength = 6;
//This calls the returnLongitude method from Settlement, and is the 'proper' way to do it.
System.out.println(townRey.returnLongitude());
//This also works.
System.out.println(townRey.longitude);
//Thanks for the help!
}
}
【问题讨论】:
-
“这是我的第一个 Java 项目。” - 请不要以小写字母开头的 Java 类名。永远不会!立即学习这一课。
-
虽然我经常听到这句话并接受它,因为它是流行的风格,但我很想知道为什么这个话题会引起人们如此强烈的愤怒。例如,让类以小写字母开头而变量以大写字母开头的标准会有什么问题?
-
@qaphla 如果那是标准的话,那也没什么问题。该标准是任意的,但普遍存在,使用错误的大小写就像拼写错误:读者通常可以理解您的意思,但需要更多的努力才能更好地解决实际问题。
-
我不确定这是一个糟糕的复制粘贴还是 OP 没有提到的错误,但
townRey的当前代码(带有双括号{{)在初始化块中包含所有内容,考虑到数组声明,这几乎肯定不是预期的。 -
我会强烈建议从 Java 初学者书籍或 Oracle 教程开始。随意在您的计算机中输入内容并期望它们通常能够正常工作不会产生您想要的结果。
标签: java static non-static