简体   繁体   中英

Java Object initialization

can't understand how to get to my object values. For example in this code line. I Got Objects for example "Money", and on button click I want to increment Money count.

 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);



    }
}

And I want to increment values for example count.

In my previous code I was doing it like CarMain.main[0] += 1;

Now with this Code I cant do it.

And cant do it like CarMain.Money.count +=1;

Well, you have implemented the correct method for doing the count.

Basically, every time you call for instance Money.count(1) , you can have the following implementation in the count method:

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

If you want the count variable to always be incremented by 1, then you dont need the count parameter. You can call the method without any arguments, and then increment by 1 within the method.

I'd like to begin by pointing out that your variable names do not conform to Java Naming Conventions and that makes your code harder to read for those of us who are trying to help you.

Next, the question is not entirely clear to me but it sounds like you want to create a graphical user interface (GUI) with a button that will increment the count of a MainHolder each time you click on it. I also think you're stuck on how to build a data model type of object that will allow you to store and modify data as needed.

Let's start with the MainHolder , which I assume is going to be your data model. A common way to create a data model object is to define the private (see Java Tutorial - Controlling Access to Members of a Class ) fields you want it to have and provide getter and/or setter methods for each field that will be publicly available to other classes (such as your 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;
    }
}

You will notice how the constructor initializes most of the internal fields and sets the id of the object to the nextId and then increments the static nextId field by one. I assumed this was the type of behavior your were looking for based on your question and your code.

Notice also that a simple pair of methods getCount and setCount are provided that allow access to the count field.

Now you can create your GUI and use your data model. I'm going to assume you'll want to follow the MVC (model view controller) pattern as that is generally considered best practice for GUI applications. So you would probably create an instance of MainHolder in a controller class, then pass your controller to your GUI instance. The controller would provide business level methods - perhaps something like addMoney(int amt)

Inside the controller you would have something similar to this:

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
        ...

Inside your GUI class you would have a listenter attached to your button that would invoke the controller's addMoney method, like this:

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

I hope this gets you moving in the right direction, good luck!

尝试将count变量声明为public,或为其构建方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM