一个java多个类,Java:具有一个类的多个对象

I am having an issue where each class has multiple objects, it is a mess of objects being created of each class and I am having errors. The 4 class files are Main, Game, Updates Building, I will show the constructors of each class and hopefully someone can help me show how to create multiple objects of one class. There are variables in game that I need to access from update and building but when I try errors are returned. How do I access the variables in game from both update and building

Main:

public class Main

{

public static void main(String[] args)

{

Game newGame = new Game();

newGame.setupGame();

Game.isRunning=true;

newGame.gameLoop();

}

}

Game:

import java.util.Scanner;

public class Game {

private Scanner input;

private Updates getUpdates;

public Game(){

this.input = new Scanner(System.in);

this.getUpdates = new Updates(this);

}

int happyness;

double money;

int population = 1000000;

}

Updates

import java.util.Scanner;

public class Updates {

private Scanner input;

private Game newGame;

Building buildBuilding = new Building();

public Updates(Game newGame){

this.newGame = newGame;

this.input = new Scanner(System.in);

}

}

Building

import java.util.Scanner;

public class Building {

public Building(){

this.input = new Scanner(System.in);

}

private Scanner input;

}

I want the building class to be able to access the variables in main as well as the update class being able to access the variables in main.

解决方案

[EDITED]

Change in Updates class:

Building buildBuilding;

public Updates(Game newGame){

this.newGame = newGame;

this.input = new Scanner(System.in);

this.buildBuilding = new Building(newGame);

}

You have accidentally called an empty constructor, so

public Building(Game newGame){

this.input = new Scanner(System.in);

this.newGame = newGame;

}

was never called, and therefore input was NULL.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值