java 如何从另一个类调用void方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26701289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:26:11  来源:igfitidea点击:

How to call a void method from another class

javaclassvoid

提问by Thanospan

I would like to call the public void display() from another class but i don't know how please help me this my program:

我想从另一个班级调用 public void display() 但我不知道如何请帮我这个我的程序:

public class SignalMap
{
    private boolean[][] signal;
    private double threshold;
    private Network net;
    private int size;

    public SignalMap(Network net, double threshold, int size)
    {
        this.net = net;
        this.threshold = threshold;
        signal = new boolean[size][size];

        for(int i = 0; i < size; i++)
            {           
                for(int j = 0; j < size; j++)
                {
                    if(net.getSignal(i, j) >= threshold)
                    {
                        signal[i][j] = false;
                    }
                    else
                    {
                        signal[i][j] = true;
                    }
                }
            }
    }

    public void display()
    {
        for(int i = 0; i < size; i++)
            {           
                for(int j = 0; j < size; j++)
                {
                    if (signal[i][j].equals(true))
                    {
                        System.out.printf("%5d ", signal[i][j]);
                    } 

                }
                System.out.println();
            }   
    }
    public double poorSignal()
    {
        int x = 0;
        for(int i = 0; i < size; i++)
            {           
                for(int j = 0; j < size; j++)
                {
                    if (signal[i][j] == true)
                    {
                        x = x + 1;
                    }   
                }
            }
        return 1/(size/x);  
    }
}

Please help tell me if I am doing something wrong. This is the question i have to answer : Write a method display in the SignalMapclass which prints the array showing those areas which have poor signal (for example, display an X if the signal is poor).

如果我做错了什么,请帮助告诉我。这是我必须回答的问题:在SignalMap类中编写一个方法 display打印显示信号较差的区域的数组(例如,如果信号较差则显示 X)。

回答by SMA

Just create instance of SignalMap and call display.

只需创建 SignalMap 的实例并调用显示。

public class MainClass {
    public static void main(String args[]) {
         SignalMap signalMap = new SignalMap(..................);
         signalMap.display();
    }
}