简体   繁体   English

System.out输入参数

[英]System.out in parameter

I have a test file and according to it I need to build my program the test file is below. 我有一个测试文件,根据它我需要构建我的程序,下面是测试文件。 However, I confused by s1.showDetails(System.out); 但是,我对s1.showDetails(System.out)感到困惑; I have never met System.out in parameter can anyone help. 我从未见过System.out的参数可以帮助任何人。 What to do with it??? 怎么办呢??? when I am trying to write showDetails() the compiler writes mistake. 当我尝试编写showDetails()时,编译器会写错误。 my student code is beneath this Thank you in advance! 我的学生代码在此下方,谢谢!

import java.util.*;
public class Q2 {
    public static void main(String [] args)
    {
        // Start on section A
        System.out.println("Question 2");
        System.out.println("Start on part A");
        Student s1 = new Student("John", "Smith", 42);
        s1.showDetails(System.out);
        Course cs = new Course("Computer science");
    }
}

public class Student {
    private String name;
    private String familyName;
    private int moduleMark;
    private int total;
    protected Student(String name, String familyName, int moduleMark)
    {
        this.name = name;
        this.familyName = familyName;
        this.moduleMark = moduleMark;
    }

    public String getName()
    {
        return name;
    }

    public String getFamilyName()
    {
        return familyName;
    }

    public int getModuleMark()
    {
        return moduleMark;
    }

    public String showDetails()
    {
        return (this.name + " " + this.familyName + " " + moduleMark + total);
        //print(name);
    }
}

System.out is a variable like every other variable. 与其他所有变量一样, System.out是一个变量。

System is a class 系统是一类

out is a public static variable inside System of type PrintStream . outPrintStream类型的System内部的public static变量。 So you can access it with System.out 因此,您可以使用System.out访问它

So System.out.println(..) is just a call to the println(..) function of a PrintStream 因此System.out.println(..)只是对PrintStreamprintln(..)函数的调用

So your function should look like: 因此,您的函数应如下所示:

public String showDetails(PrintStream stream) {
 ...
}

The error essentially means that the compiler didn't find the method with the name showDetails that takes an argument of type PrintStream . 该错误本质上意味着编译器找不到名称为showDetails的方法,该方法带有PrintStream类型的参数。 You have no need to pass the System.out to the showDetails() method. 您无需将System.out传递给showDetails()方法。 The correct way writing the showDetails() is below. 下面是编写showDetails()的正确方法。 Read about System.out . 了解有关System.out的信息

public void showDetails()
{
   System.out.println(this.name + " " + this.familyName + " " + moduleMark + total);
}

The error pretty much describes what the problem is. 该错误几乎说明了问题所在。

The method showDetails() in the type Student is not applicable for the arguments (PrintStream) at Q2.main(Q2.java:9) Student类型的showDetails()方法不适用于Q2.main(Q2.java:9)上的参数(PrintStream)

The program tries to call your method with System.out which happens to be a PrintStream. 该程序尝试使用碰巧是PrintStream的System.out调用您的方法。 So, change your showDetails to 因此,将showDetails更改为

public String showDetails(PrintStream out) {
    out.println(this.name + " " + this.familyName + " " + moduleMark + total);
}

This allows the tester (I assume there is a program which tests your assignment for correctness) to give you any PrintStream, either System.out or any other PrintStream. 这使测试人员(我假设有一个程序可以测试您的分配是否正确)可以为您提供任何PrintStream,可以是System.out或任何其他PrintStream。

Your program can't compile. 您的程序无法编译。

Your main is using a method named showDetails that takes a PrintStream as a parameter (System.out). 您的主要方法是使用名为showDetails的方法,该方法将PrintStream作为参数(System.out)。 And you are defining only a method named showDetails without parameter. 而且,您仅定义了一个没有参数的名为showDetails的方法。

Look at the doc of System.out . 查看System.out文档 It sa field like any other of the class System. 与其他系统类一样,它也是一个领域。 Indeed it's somewhat special as it is static but that doesn't change the game that much... 确实,它有些特殊,因为它是静态的,但这并不会改变游戏的多少...

So write a method with the correct parameter list and you will get closer. 因此,编写具有正确参数列表的方法,您将更加接近。

public String showDetails(PrintStream stream) {
 //your code here
}

showDetails should write to the stream passed in parameter. showDetails应该写入传入的参数流。

While you are at learning programming. 在学习编程时。 Try to separate query from command . 尝试将查询与命令分开 It's a good principle : a method should do one thing : either do something to your current object using a parameter or answer a query about the state of your current object. 这是一个很好的原则:方法应该做一件事:要么使用参数对当前对象做某事,要么回答有关当前对象状态的查询。 Not both. 不是都。 Here you return a string and require a stream... You should better split that into 2 methods : one to get the string and then as second one to call stream.write on that string. 在这里,您返回一个字符串并需要一个流...您最好将其分成两种方法:一种方法获取字符串,然后第二种方法对该字符串调用stream.write。

public String toString() {
 //your code here
}

public void showDetails(PrintStream stream) {
 //your code here
}

Since the print is commented out from your showDetails message there are 2 obvious solutions 由于print是从showDetails消息中注释掉的,因此有2种明显的解决方案

  1. The showDetails message is not meant to print out anything (despite the name) and you should just print it out in the main method showDetails消息并不意味着要打印出任何内容(尽管有名称),而应该只在main方法中将其打印出来

     System.out.println( s1.showDetails() ); 
  2. The showDetails message should print the String to any PrintStream , and then you have to adjust both the signature of that method as well as the implementation showDetails消息应将String打印到任何PrintStream ,然后必须调整该方法的签名以及实现

     public String showDetails(PrintStream stream) { stream.println( ... ); } 

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

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