1.如果可能尽量使用接口来编程
.net框架包括类和接口,在编写程序的时候,你可能知道正在用.net的哪个类。然而,在这种情况下如果你用.net支持的接口而不是它的类来编程时,代码会变得更加稳定、可用性会更高。请分析下面的代码:
| private void loadlist (object [] items, listbox l) { for (int i = 0; i < items.length;i++) l.items.add (items[i].tostring ()); } |
| private void loadlist (icollection items,listbox l) { foreach (object o in items) l.items.add (o.tostring ()); } |
| private int themonth = 0; [xmlattribute ("month")] public int month { get { return themonth; } set { themonth = value; } } |
| public class keyboardprocessor { private ongetline thefunc = null; public ongetline ongetlinecallback { get { return thefunc; } set { thefunc = value; } } public void run (){ // read input. // if there is any listeners, publish: string s; do { s = console.readline (); if (s.length == 0) break; if (thefunc != null){ system.delegate [] funcs =thefunc.getinvocationlist(); foreach (ongetline f in funcs) { try { f (s); } catch (exception e) { console.writeline ("caught exception: {0}", e.message); } } } } while (true); } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器