简体   繁体   English

"如何从对象的构造函数中的另一个类访问布尔值?"

[英]How do I access a boolean from another class in the constructor of an object?

So I have a main class, and in this class I have a boolean called test.所以我有一个主类,在这个类中我有一个名为 test 的布尔值。 I want to be able to flip it either on or off after a certain amount of time, so I have a class called BooleanFlipCounter.我希望能够在一定时间后打开或关闭它,所以我有一个名为 BooleanFlipCounter 的类。 Below以下

public class BooleanFlipCounter implements Runnable {
    int seconds;
    boolean booleanToFlip;
    boolean setOnOrOff;

    // Seconds is seconds before flip.
    // booleanToFlip is where you define where you use it which boolean to use.
    // setOnOrOff is true or false, whether it flips it to on or off after a
    // certain time.

    public BooleanFlipCounter(int secondss, boolean booleanToFlips, 
                              boolean setOnOrOffs) {
        seconds = secondss;
        booleanToFlip = booleanToFlips;
        setOnOrOff = setOnOrOffs;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (seconds <= 0) {

            System.out.println("1: " + booleanToFlip + " : " + setOnOrOff);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            seconds--;
            System.out.println("Minused");

        }

        if (setOnOrOff == true) {
            Main.test = true;
            System.out.println("2: " + booleanToFlip + " : " + setOnOrOff);
        } else if (setOnOrOff == false) {
            Main.test = false;
            System.out.println("3: " + booleanToFlip + " : " + setOnOrOff);
        }

    }

}

I want to be able to access "test" when I create a new BooleanFlipCounter.当我创建一个新的 BooleanFlipCounter 时,我希望能够访问“测试”。 How do I access it as the boolean object, and not as an actual boolean?如何将它作为布尔对象访问,而不是作为实际的布尔值? If that makes any sence.如果这有任何意义。

A boolean variable is not an object, and you can't access it "as" an object. boolean变量不是对象,您不能“作为”对象访问它。 If you want a reference to a boolean variable, you need to use a reference to the object that contains it.如果要引用boolean变量,则需要使用对包含它的对象的引用。

You could define an interface:您可以定义一个接口:

public interface Testable {
    boolean isTest();
    void setTest(boolean test);
}

Then you can make your Main class implement Testable in terms of its internal test flag, and make BooleanFlipCounter take a reference to a Testable object that it should manipulate.然后,您可以让Main类根据其内部test标志实现Testable ,并使BooleanFlipCounter引用它应该操作的Testable对象。

(Note that this requires you to create an instance of your Main class, or at least of something that implements the Testable interface. If the Main.test flag is currently a static variable, consider making it non-static. You can have your static main method just create a new Main() and call a method on it to do the real work.) (请注意,这需要您创建Main类的实例,或者至少创建实现Testable接口的实例。如果Main.test标志当前是静态变量,请考虑将其设为非静态变量。您可以拥有静态main方法只是创建一个new Main()并在其上调用一个方法来完成真正的工作。)

[r}>>sys.pixelchanger.letterenderer.render("hello world");
[r}>>sys.pixelchanger.letterenderer.rener("hello world");

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

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