Topic: 类中类的main()如何运行 |
Print this page |
1.类中类的main()如何运行 | Copy to clipboard |
Posted by: laihamawang Posted on: 2003-04-01 15:22 请问Test类中的main()如何运行,这个程序我可以编译通过,但不知道如何去运行,?请指教。 import java.io.*; /** * This class is a BufferedReader that filters out all lines that * do not contain the specified pattern. **/ public class GrepReader extends BufferedReader { String pattern; // The string we are going to be matching. /** Pass the stream to our superclass, and remember the pattern ourself */ public GrepReader(Reader in, String pattern) { super(in); this.pattern = pattern; } /** * This is the filter: call our superclass's readLine() to get the * actual lines, but only return lines that contain the pattern. * When the superclass readLine() returns null (EOF), we return null. **/ public final String readLine() throws IOException { String line; do { line = super.readLine(); } while ((line != null)&& line.indexOf(pattern) == -1); return line; } /** * This class demonstrates the use of the GrepReader class. * It prints the lines of a file that contain a specified substring. **/ public static class Test { public static void main(String args[]) { try { if (args.length != 2) throw new IllegalArgumentException("Wrong number of args"); GrepReader in=new GrepReader(new FileReader(args[1]), args[0]); String line; while((line = in.readLine()) != null) System.out.println(line); in.close(); } catch (Exception e) { System.err.println; System.out.println("Usage: java GrepReader$Test" + " <pattern> <file>"); } } } } |
2.Re:类中类的main()如何运行 [Re: laihamawang] | Copy to clipboard |
Posted by: laihamawang Posted on: 2003-04-01 15:24 还有一个问题, 为什么main()必须在Test中,而不在GrepReader中? |
3.Re:类中类的main()如何运行 [Re: laihamawang] | Copy to clipboard |
Posted by: luoq_dl Posted on: 2003-04-01 15:48 如下: java GrepReader$Test |
4.Re:类中类的main()如何运行 [Re: laihamawang] | Copy to clipboard |
Posted by: luoq_dl Posted on: 2003-04-01 15:50 为什么main()必须在Test中,而不在GrepReader中? 因为这个方法是测试GrepReader类的,而Test类是对这个方法的一个封装。其实没有什么必须的问题,应当只是一个实现的问题 |
5.Re:类中类的main()如何运行 [Re: laihamawang] | Copy to clipboard |
Posted by: jiangns3000 Posted on: 2003-04-01 16:23 这个程序代码(指Test这个 Inner class)我认为不好: 1)JAVA中Inner class 主要用于方便事件处理的书写及匿名类(如定义小的线程类),不应该被滥用。本例似有滥用之嫌。 2)本例中的Test类又被定义为static,从而成为一个顶层类,这是Inner class有时要可能避免的。 3)其实几乎所有的类都可能放上main()方法。(只要你愿意),从而使该类有可能作为一个程序运行。本例中Test类完全可以去除,将main() 直接放在GrepReader类中。 4)所有Inner class 在编译时都单独有一个.class文件。其文件名的取法luoq_dll已说明。 |
6.Re:类中类的main()如何运行 [Re: laihamawang] | Copy to clipboard |
Posted by: laihamawang Posted on: 2003-04-01 16:32 十分感谢前辈指点,:) |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |