2012-01-03 62 views
0

我有一个测试文件,根据它我需要构建我的程序测试文件 下面。但是,我困惑于s1.showDetails(System.out);我从来没有遇到过 参数System.out任何人都可以帮助。该怎么办?当我试图编写showDetails()时,编译器写入错误。我的学生代码是在这之下谢谢你提前!System.out参数

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); 
    } 
} 
+0

您需要发布你所得到的错误。另外,如果这是作业,那么添加'作业'标记 – 2012-01-03 21:58:37

+0

线程“main”中的异常java.lang.Error:未解决的编译问题: \t Student类型的showDetails()方法不适用于参数(PrintStream) \t at Q2.main(Q2.java:9) – 2012-01-03 21:59:44

+0

这不是作业 – 2012-01-03 22:00:18

回答

1

该错误几乎描述了问题所在。

The method showDetails() in the type Student is not applicable for the arguments (PrintStream) at Q2.main(Q2.java:9)

程序试图用System.out调用您的方法,这恰好是PrintStream。因此,改变你的showDetails到

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

这使得测试仪(我假设有一个程序,测试你是否正确分配)给你任何的PrintStream,无论是System.out的或任何其他的PrintStream。

1

错误基本上意味着,编译器不使用名称找到方法showDetails这需要PrintStream类型的参数。您无需将System.out传递给showDetails()方法。下面是编写showDetails()的正确方法。阅读关于System.out

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

System.out是像其他变量的变量。

System是一类

out是内部PrintStream类型的System一个publicstatic变量。所以,你可以用System.out

访问它所以System.out.println(..)仅仅是一个PrintStream

println(..)函数的调用,所以你的函数应该是这样的:

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

你的程序不能编译。

您的主要使用名为showDetails的方法将PrintStream作为参数(System.out)。而且您只定义了一个名为showDetails的方法,不带参数。

看看doc of System.out。这是一个像任何其他类系统一样的领域。事实上,它有点特别,因为它是静态的,但并不会改变游戏那么多......

因此,编写一个正确的参数列表的方法,你会更接近。

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

showDetails应该写入传入参数中的流。

当你在学习编程。尝试to separate query from command。这是一个很好的原则:一个方法应该做一件事:或者使用参数对当前对象进行一些操作,或者回答关于当前对象状态的查询。不是都。 在这里,你返回一个字符串,并需要一个流......你应该更好地将它分成两个方法:一个获取字符串,然后第二个调用该字符串的stream.write。

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

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

由于print从您的showDetails消息注释掉有2个明显的解决方案

  1. 并不意味着showDetails信息打印出任何东西(尽管名字),你应该只把它打印出来的主要方法

    System.out.println(s1.showDetails()); 
    
  2. showDetails消息应打印字符串任何PrintStream,然后你必须调整该方法的两个签名以及实施

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