简体   繁体   English

如何使用另一个类的方法

[英]How to use a method from another class

I want to add this method ( which is located in my Houses class)我想添加这个方法(它位于我的 Houses 类中)

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)到这个班级(ghetto 班级,这是我的主要班级)

    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:所以我想要的是在我的主类 ghetto 中使用 step 方法(位于我的 Houses 类中),在这里它给出错误:

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

It says the constructor Houses() is undefined.它说构造函数 Houses() 未定义。

you need to add a default constructor for you Houses class.您需要为Houses类添加一个默认构造函数。

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.您可能有一个,看看您的类,您可能使用 netBeans 或 Eclipse 自动生成它,因此它将所有类属性作为参数。

Take a look at OOP principes :看看OOP原理:

A class House A类房子

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:如您在其他问题中提供的代码所示, Houses的构造函数是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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