每次尝试制作新程序时,我都会遇到 运行 相同的问题
I keep running into the same problems every time I try to make a new program
我当前的程序是一个简单的游戏..我正在努力让它帮助弄清楚如何正确使用 OOP。但每次我稍微接触一个新程序时,我都会 运行 遇到同样的问题:
我创建了一个实例用于我的游戏。这个实例可以是任何东西——主要英雄、怪物,等等。一切顺利,实例正是我想要的。也许我稍微操纵了一下。
然后我尝试使用不同的 class,我想我创建了一个不同的 class 的实例,试图进一步操纵原始实例。也许我最初创建了我的英雄并更改了他的统计数据,现在我正试图让我的英雄根据所选择的统计数据说些什么。
这一点一直是我遇到的障碍。我似乎无法在这里做我想做的事——我不能使用我制作的原始实例,因为它只是一个实例,我不知道如何操作它(或者即使我应该操作)在一个新的 class.
中
所以..这是理解 OOP 的基础设计problem/lack。
但我觉得我对如何实际编程东西有很好的把握。我一直在阅读和阅读并获得建议,但我似乎无法越过这个障碍。
我知道这个网站上的人似乎不喜欢这样的 post 但也许这里有人可以识别我没有掌握的东西。
这是我正在谈论的一个例子,我正在尝试制作的游戏。
package pkgnew.stupid.game;
import java.util.Scanner;
/**
*
* @author whyguy2
*/
public class Hero {
public static int heroattack = 1;
public static int herospeed = 1;
public static int heroarmorpen = 1;
public static int heroarmor = 1;
public static int herohealth = 5;
public static String inputstat;
public static int herothepoints = 0;
Hero(int points){
spendpoint(points);
}
public void attack(){
}
public void die(){
System.exit(0);
}
public void winbattle(){
System.out.println("You win the battle and have one new point to spend!");
spendpoint(1);
new NewEncounter();
}
public void levelup(){
System.out.println("You have leveled up and receive 5 new points to spend");
spendpoint(5);
new NewEncounter();
}
public void spendpoint(int points){
for(int x=0; x<points; x++){
System.out.println("Available Points: " + points);
System.out.println("Available Attributes:");
System.out.println("attack: " + heroattack);
System.out.println("speed: " + herospeed);
System.out.println("armorpen: " + heroarmorpen);
System.out.println("armor: " + heroarmor);
System.out.println("health: " + herohealth);
System.out.println(points);
System.out.println(x);
Scanner keyboard = new Scanner( System.in );
inputstat = keyboard.next( );
if(inputstat.equals("attack")){
heroattack = heroattack + 1;
}
else if(inputstat.equals("speed")){
herospeed = herospeed + 1;
}
else if(inputstat.equals("armorpen")){
heroarmorpen = heroarmorpen + 1;
}
else if(inputstat.equals("armor")){
heroarmor = heroarmor + 1;
}
else if(inputstat.equals("health")){
herohealth = herohealth + 5;
}
else{
System.out.println("Please pick one of the stats");
x = x-1;
}
}
}
}
public class StartGame {
StartGame(){ //runs through output for a new game
System.out.println("Welcome to the game");
System.out.println("Pick your hero's starting stats");
Hero thishero = new Hero(10); //spends your points
System.out.println("Let's Begin!/n/n");
new NewEncounter(); //goes into the encounter loops
}
}
此时我尝试在 NewEncounter class 中编写一次遭遇战,但这是不可能的,因为我无法使用我创建的英雄实例。而且我很确定我的设计首先很糟糕,我想我已经读过你应该首先尝试尽可能少地使用静态变量。我仍在阅读,试图理解这一点,但我已经阅读了很多,但我觉得没有任何帮助。我实际上认为 "hands on" 更大规模 project/tutorial 可能对我有帮助,但我不知道。无论如何,感谢您的帮助,并对 long/blog-like post.
感到抱歉
在另一个 class 中使用一个实例的标准方法是存储对它的引用或将其传递给存储或使用它的方法。
例如:
class Encounter {
private final Hero mainHero;
private final List<Hero> participants;
public Encounter(Hero mainHero) {
this.mainHero = mainHero;
participants = new ArrayList<>();
}
public void addParticipant(Hero hero) {
participants.add(hero);
}
}
允许:
Encounter encounter = new Encounter(hero);
或:
encounter.addParticipant(hero);
这些可以在 class 中使用。例如:
class Encounter {
public void moveAllParticipants() {
participants.forEach(Hero::move);
}
}
我注意到您使用了很多 public 静态 class 变量。这是不寻常的,几乎没有充分的理由使用它们。一是定义常量。如果这是您的意图,那么我建议您使用以下标准格式:
private static final int HERO_ATTACK = 1;
如果您打算将它们作为实例变量(即每个英雄不同),那么它们不应该是静态的,也不应该 public。
这是各种 Java 教程中很好地涵盖的领域。我建议解决其中一个问题,如果您还有其他问题,请返回此处。
我当前的程序是一个简单的游戏..我正在努力让它帮助弄清楚如何正确使用 OOP。但每次我稍微接触一个新程序时,我都会 运行 遇到同样的问题:
我创建了一个实例用于我的游戏。这个实例可以是任何东西——主要英雄、怪物,等等。一切顺利,实例正是我想要的。也许我稍微操纵了一下。
然后我尝试使用不同的 class,我想我创建了一个不同的 class 的实例,试图进一步操纵原始实例。也许我最初创建了我的英雄并更改了他的统计数据,现在我正试图让我的英雄根据所选择的统计数据说些什么。
这一点一直是我遇到的障碍。我似乎无法在这里做我想做的事——我不能使用我制作的原始实例,因为它只是一个实例,我不知道如何操作它(或者即使我应该操作)在一个新的 class.
中所以..这是理解 OOP 的基础设计problem/lack。
但我觉得我对如何实际编程东西有很好的把握。我一直在阅读和阅读并获得建议,但我似乎无法越过这个障碍。
我知道这个网站上的人似乎不喜欢这样的 post 但也许这里有人可以识别我没有掌握的东西。
这是我正在谈论的一个例子,我正在尝试制作的游戏。
package pkgnew.stupid.game;
import java.util.Scanner;
/**
*
* @author whyguy2
*/
public class Hero {
public static int heroattack = 1;
public static int herospeed = 1;
public static int heroarmorpen = 1;
public static int heroarmor = 1;
public static int herohealth = 5;
public static String inputstat;
public static int herothepoints = 0;
Hero(int points){
spendpoint(points);
}
public void attack(){
}
public void die(){
System.exit(0);
}
public void winbattle(){
System.out.println("You win the battle and have one new point to spend!");
spendpoint(1);
new NewEncounter();
}
public void levelup(){
System.out.println("You have leveled up and receive 5 new points to spend");
spendpoint(5);
new NewEncounter();
}
public void spendpoint(int points){
for(int x=0; x<points; x++){
System.out.println("Available Points: " + points);
System.out.println("Available Attributes:");
System.out.println("attack: " + heroattack);
System.out.println("speed: " + herospeed);
System.out.println("armorpen: " + heroarmorpen);
System.out.println("armor: " + heroarmor);
System.out.println("health: " + herohealth);
System.out.println(points);
System.out.println(x);
Scanner keyboard = new Scanner( System.in );
inputstat = keyboard.next( );
if(inputstat.equals("attack")){
heroattack = heroattack + 1;
}
else if(inputstat.equals("speed")){
herospeed = herospeed + 1;
}
else if(inputstat.equals("armorpen")){
heroarmorpen = heroarmorpen + 1;
}
else if(inputstat.equals("armor")){
heroarmor = heroarmor + 1;
}
else if(inputstat.equals("health")){
herohealth = herohealth + 5;
}
else{
System.out.println("Please pick one of the stats");
x = x-1;
}
}
}
}
public class StartGame {
StartGame(){ //runs through output for a new game
System.out.println("Welcome to the game");
System.out.println("Pick your hero's starting stats");
Hero thishero = new Hero(10); //spends your points
System.out.println("Let's Begin!/n/n");
new NewEncounter(); //goes into the encounter loops
}
}
此时我尝试在 NewEncounter class 中编写一次遭遇战,但这是不可能的,因为我无法使用我创建的英雄实例。而且我很确定我的设计首先很糟糕,我想我已经读过你应该首先尝试尽可能少地使用静态变量。我仍在阅读,试图理解这一点,但我已经阅读了很多,但我觉得没有任何帮助。我实际上认为 "hands on" 更大规模 project/tutorial 可能对我有帮助,但我不知道。无论如何,感谢您的帮助,并对 long/blog-like post.
感到抱歉在另一个 class 中使用一个实例的标准方法是存储对它的引用或将其传递给存储或使用它的方法。
例如:
class Encounter {
private final Hero mainHero;
private final List<Hero> participants;
public Encounter(Hero mainHero) {
this.mainHero = mainHero;
participants = new ArrayList<>();
}
public void addParticipant(Hero hero) {
participants.add(hero);
}
}
允许:
Encounter encounter = new Encounter(hero);
或:
encounter.addParticipant(hero);
这些可以在 class 中使用。例如:
class Encounter {
public void moveAllParticipants() {
participants.forEach(Hero::move);
}
}
我注意到您使用了很多 public 静态 class 变量。这是不寻常的,几乎没有充分的理由使用它们。一是定义常量。如果这是您的意图,那么我建议您使用以下标准格式:
private static final int HERO_ATTACK = 1;
如果您打算将它们作为实例变量(即每个英雄不同),那么它们不应该是静态的,也不应该 public。
这是各种 Java 教程中很好地涵盖的领域。我建议解决其中一个问题,如果您还有其他问题,请返回此处。