简体   繁体   中英

How to use a method from another class

I want to add this method ( which is located in my Houses class)

public void step() {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            // Who are my neighbors
            House[][] ns = neighbors(houses[i][j]);

            // How many are red, blue
            int countRed = 0;
            int countBlue = 0;
            for (int n = 0; n < 8; n++) {
                if (ns[j][j].who == HouseType.Red) {
                    countRed = countRed + 1;
                }
                if (ns[j][j].who == HouseType.Blue) {
                    countBlue = countBlue + 1;
                }
            }
            // Decide to stay or move
            if (houses[i][j].decide(countRed, countBlue)) {
                houses[i][j].move(ns);
            }
        }
    }
}

To this class ( ghetto class which is my main class)

    startButton = new JButton("start");
    add(startButton);
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Create a timer
            time = new javax.swing.Timer((int) (1000 * deltaT), this);
            time.start();
            // Add a listener for the timer - which is the step method
            if (e.getSource() == time) 
            {
                Houses h = new Houses();
                h.step();
                //Houses.step();
            }

        }
    });

So what I want is that to use the step method (that is located in my Houses class ) in my main class ghetto, here where it is giving the error:

           Houses h = new Houses();
           h.step();

It says the constructor Houses() is undefined.

you need to add a default constructor for you Houses class.

In Houses class :

public Houses() {

}

You might have one, take a look at your class, you probably autogenerated it with netBeans or Eclipse so it took all class attributes as parameter.

Take a look at OOP principes :

A class House

public class House {
    int numberOfPeopleInHouse = 2;
    int numberOfDog = 0;

    public House() {
        //default constructor does nothing but creating a house.
    }
    public House(int dogCount) {
        this.numberOfDog = dogCount;
        /* This particular constructor take a int as parameter, and instead of creating
         * a simple house, we create an house with a modified dog count.
         */
    }
    public void addPeople(int numberOfPeople) {
        this.numberOfPeopleInHouse = this.numberOfPeopleInHouse + numberOfPeople;
    }
}

In your main

static void main(string[] args) {
    House firstHouse = new House(1);
    //firstHouse contains 2 people and 1 dog

    House secondHouse = new House();
    //secondHouse contains 2 people and 0 dog

    secondHouse.addPeople(2);
    //secondHouse contains 4 people and 0 dog
}

As shown in the code you provided in your other question , the constructor for Houses is:

public Houses(int size, int blue, int red) { ... }

It expects those three arguments, so you have to provide them, which seem to be, the size of the sides of your square house grid, and the initial number of blue and red houses. So maybe:

Houses h = new Houses(5, 3, 3);
h.step();

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