简体   繁体   English

关于Java程序中的语句的Noob问题

[英]Noob question about a statement in a Java program

I am beginner to java and was trying out this code puzzle from the book head first java which I solved as follows and got the output correct :D 我是Java的初学者,并且首先从本书开头的Java尝试了这个代码难题,我如下解决了该问题并得到了正确的输出:D

class DrumKit
    {
        boolean topHat=true;
        boolean snare=true;
        void playSnare()
            {
                System.out.println("bang bang ba-bang");
            }
        void playTopHat()
            {
                System.out.println("ding ding da-ding");
             }


    }

public class DrumKitTestDriver
    {

        public static void main(String[] args)

            {
                DrumKit d =new DrumKit();

                if(d.snare==true)

                    {
                        d.playSnare();
                    }

                                //d.snare=false;

                   d.playTopHat();

            }

    }

Output is :: 输出为::

bang bang ba-bang ding ding da-ding 棒棒棒棒棒哒

Now the problem is that in that code puzzle one code snippet is left that I did not include..it's as follows 现在的问题是,在该代码难题中,剩下一个我不包括的代码段。其内容如下

d.snare=false; d.snare = false;

Even though I did not write it , I got the output like the book. 即使我没有写它,我也得到了像本书一样的输出。 I am wondering why is there need for us to set it's value as false even when we know the code is gonna run without it too !?? 我想知道为什么即使我们知道代码也会在没有它的情况下运行,为什么我们需要将其值设置为false?

I am wondering what the coder had in mind ..I mean what could be the possible future use and motive behind doing this ? 我想知道编码人员的想法是什么。.我的意思是,这样做的未来用途和动机可能是什么?

I am sorry if it's a dumb question. 很抱歉,这是一个愚蠢的问题。 I just wanna know why or why not to include that particular statement ? 我只想知道为什么或为什么不包括该声明? It's not like there's a loop or something that we need to come out of. 这并不是说存在一个循环或我们需要解决的问题。 Why is that statement there ? 为什么会有那句话?

它可能只是在这里演示如何更改公共成员变量。

where is 哪里

d.snare=false;

If it is before the if condition then it would change the output by affecting state of the variable inside class d, it would now just post. 如果它在if条件之前,那么它将通过影响d类中变量的状态来更改输出,现在它将仅发布。

ding ding da-ding The if condition would be bypassed, however if it is after the if condition, it will not have any effect since its value is evaluated only in the if condition. ding ding da-ding如果if条件将被绕过,但是如果它在if条件之后,则它将没有任何效果,因为它的值仅在if条件中被评估。

written in Main or inside the class; 用Main或课堂内写的;

From the code you have posted the output is correct. 根据您发布的代码,输出正确。

I'm quite sure there is NO reason to change a variable/state/whatever if it is definitive not used afterwards! 我敢肯定没有理由去改变什么,如果它是明确的事后又没有使用的变量/状态/!

Regards 问候

You should let DrumKit handle the logic and let the main class just do the settings on DrumKit. 您应该让DrumKit处理逻辑,并让主类在DrumKit上进行设置。 Something like this: 像这样:

class DrumKit
{
    boolean topHat=true;
    boolean snare=true;
    void playSnare() {
            System.out.println("bang bang ba-bang");
    }
    void playTopHat() {
            System.out.println("ding ding da-ding");
    }

    void play(){
            if (snare){
                     playSnare();
            }
            if (topHat){
                     playTopHat();
            }
    }
}

public class DrumKitTestDriver {

    public static void main(String[] args) {
            DrumKit d =new DrumKit();
            d.snare = false;
            d.play();
         }
}

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

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