2016-10-22 313 views
0

无法理解如何获取我的对象值。例如在这个代码行中。我有对象,例如“钱”,并在按钮单击我想增加钱数。Java对象初始化

package com.crelix.crelix; 

public class MainHolder { 

    int id; 
    String name; 
    int count; 

    public void id(int id) { 

    } 

    public MainHolder(String name) { 


    } 


    public void count(int count) { 

    } 


    public static void main(String args[]) { 

     MainHolder Money = new MainHolder("Money: "); 
     MainHolder MoneyClicks = new MainHolder("Money Clicks: "); 
     MainHolder Boxes = new MainHolder("Boxes: "); 
     MainHolder BoxClicks = new MainHolder("Boxes Clicks: "); 
     MainHolder BoxLevel = new MainHolder("Box Level: "); 
     MainHolder PlayerLevel = new MainHolder("Player Level: "); 
     MainHolder GarageLevel = new MainHolder("Garage Level: "); 
     MainHolder GarageSlots = new MainHolder("Garage Slots: "); 

     Money.id(1); 
     Money.count(0); 

     MoneyClicks.id(2); 
     MoneyClicks.count(0); 

     Boxes.id(3); 
     Boxes.count(0); 

     BoxClicks.id(4); 
     BoxClicks.count(0); 

     BoxLevel.id(5); 
     BoxLevel.count(1); 

     PlayerLevel.id(6); 
     PlayerLevel.count(1); 

     GarageLevel.id(7); 
     GarageLevel.count(1); 

     GarageSlots.id(8); 
     GarageSlots.count(25); 



    } 
} 

而我想增加例如计数值。

在我以前的代码,我喜欢CarMain.main[0] += 1;

本守则我不能做它做它现在。

并且不能做到像CarMain.Money.count +=1;

回答

-2

尝试声明计数变量为public,或建立一个方法,它

0

嗯,你已经实现做计数的正确方法。

基本上,每次调用例如Money.count(1)时间,您可以在计数方法实现如下:

// This function will increment the count by the entered count variable. 
public void function count(int count){ 
    this.count += count; 
} 

如果你想计数变量总是通过1递增,那么你不需要的计数参数。你可以调用没有任何参数的方法,然后在方法内加1。

0

我想先指出你的变量名称不符合Java Naming Conventions,这使得你的代码更难以阅读,因为我们这些正试图帮助你的人。

接下来,这个问题对我来说并不完全清楚,但它听起来像您想要创建一个带有按钮的图形用户界面(GUI),每次点击该按钮都会增加一个MainHolder的计数。我也认为你一直在坚持如何构建一个数据模型类型的对象,这将允许你根据需要存储和修改数据。

让我们从MainHolder开始,我假设它将成为您的数据模型。创建数据模型对象的常用方法是定义您希望它具有的private(请参见Java Tutorial - Controlling Access to Members of a Class)字段,并为每个字段提供getter和/或setter方法,这些方法将为其他类(例如GUI)公开提供。

public class MainHolder { 

    private static int nextId = 1; 
    private int id; 
    private String name; 
    private int count; 

    public MainHolder(String name) { 
     count = 0; 
     this.name = name; 
     id = nextId; 
     nextId++; 
    } 

    public void setCount(int count) { 
     this.count = count; 
    } 

    public int getCount(){ 
     return count; 
    } 
} 

你会发现构造函数初始化如何最内部的领域,并把对象的nextIdid然后逐一递增staticnextId领域。我认为这是你根据你的问题和你的代码寻找的行为类型。

还要注意,提供了一对简单的方法getCountsetCount,允许访问count字段。

现在您可以创建GUI并使用您的数据模型。我将假设你会想要遵循MVC (model view controller)模式,因为这通常被认为是GUI应用程序的最佳实践。所以你可能会在控制器类中创建一个MainHolder的实例,然后将你的控制器传递给你的GUI实例。该控制器将提供企业级的方法 - 也许像addMoney(int amt)

你会与此类似控制器内部:

public class Controller{ 
    private MainHolder money; 
    ... 

    public Controller(){ 
     money = new MainHolder("Money: "); 
    } 
    ... 

    public addMoney(int amt){ 
     int amount = money.getCount() + amt; 
     money.setCount(amount); 
    } 
    ... 

    public static void main(String args[]) { 
     Controller controller = new Controller(); 
     MyGui myGui = new MyGui(controller); 
     //Invoke appropriate method(s) to show the GUI 
     ... 

内,您的GUI类,你将有一个listenter连接到您的按钮将调用控制器的addMoney方法,像这样:

addMoneyButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      controller.addMoney(1); 
     } 
    }); 

我希望这可以让你在正确的方向前进,祝你好运!