Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在组成方面的预期产出差异_Java_Inheritance_Printing_Output_Composition - Fatal编程技术网

Java 在组成方面的预期产出差异

Java 在组成方面的预期产出差异,java,inheritance,printing,output,composition,Java,Inheritance,Printing,Output,Composition,我是编程新手,所以当我遇到障碍时,我正在上一堂在线课程(Java基础知识第1部分,John Sonmez在Pluralsight上的基本作文。我很确定你可以开始试用,因为我无法直接链接到视频)。在这里调用myPrinter对象上的print(1)方法时,讲师得到输出“加载更多纸张!”,而我继续得到输出“我的打印机已打开!”。我相信我写下的代码和他的一模一样,但显然我遗漏了一些东西。根据我推导的代码逻辑(这显然是错误的),它应该打印“我的打印机已打开!”,因为PaperTray类中的pages变量

我是编程新手,所以当我遇到障碍时,我正在上一堂在线课程(Java基础知识第1部分,John Sonmez在Pluralsight上的基本作文。我很确定你可以开始试用,因为我无法直接链接到视频)。在这里调用myPrinter对象上的print(1)方法时,讲师得到输出“加载更多纸张!”,而我继续得到输出“我的打印机已打开!”。我相信我写下的代码和他的一模一样,但显然我遗漏了一些东西。根据我推导的代码逻辑(这显然是错误的),它应该打印“我的打印机已打开!”,因为PaperTray类中的pages变量为0,这使得该类中的isEmpty方法返回false(因为0>0为false),这是与“copies>0”一起出现的条件条件仅允许执行打印方法中的while循环。另一方面,这与练习的逻辑背道而驰,这表明在运行PaperTray类的addPaper方法之前,我会被告知加载更多纸张,这将使pages=1,这意味着isEmpty方法将解析为true和myPrinter。print(1)将适当地输出“加载更多纸张!”(如果我手动将页面更改为1,它会这样做!)。我回到intelliJ中,按照我看到的方式对其进行编码,一直到编码风格,但现在我在这里对其进行了格式化。我是否忽略了一些微小的东西

    public class Main {
              public static void main(String[] args) {
                    Printer myPrinter = new Printer(true, "MY PRINTER");        
                    myPrinter.print(1);  //For the instructor this outputs "Load more paper!" while my output is "MY PRINTER is On!"      
                }     
            }
打印机类

public class Printer extends Machine{

            private String modelNumber;
            private PaperTray paperTray = new PaperTray(); 

            public Printer(boolean isOn, String modelNumber)
            {
                super(isOn);
                this.modelNumber = modelNumber;
            }
            public void print(int copies)
            {
                String onStatus = ""; 
                if(isOn)
                    onStatus = " is On!";
                else
                    onStatus=" is Off!";
                  //Uses Machine to determine onStatus
                String textToPrint = modelNumber + onStatus;

                while( copies > 0 && !paperTray.isEmpty() ) //while copies > 0 and paperTray.isEmpty() is false
                {
                    System.out.println(textToPrint);  
                    copies--;   true
                }
                if( paperTray.isEmpty() ) // if paperTray.isEmpty is true
                {
                    System.out.println("Load more paper!"); 
                }
            }
PaperTray类,用于模拟打印机的纸盘

class PaperTray{
              int pages = 0;
              public void addPaper(int count){
                pages += count;
              }
              public void usePage(){
                pages--;
              }
              public boolean isEmpty(){
                return pages > 0;
              }//Since 0 > 0 is false, only the while loop of the Printer's print method executes! 
            }
用于对打印机进行分类的机器类

class Machine 
{


错误出现在
纸盒的
isEmpty
中--您通过测试页数是否大于零来测试它是否为空,这是空的相反条件。方法体应为:

return pages == 0;

看到了吗,虽然我已经把返回页
return pages == 0;