我学习使用java的一点体会(3)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • java基本类库的学习

         曾经面试过一些java程序员,我出的题目是在使用java时,经常用到哪些类,在哪个包里。很少有人能答好,甚至有人做了2年java程序都没有回答出,他们会用java,但必须依赖工具(指java集成开工具),我觉得他们可以编写出程序,但不能编写出特别好得程序。

        我认为作为java程序员必须掌握java类库的两个包:java.lang和java.util这两个包。java.lang包提供的是java编程要用到的基本类包,java程序员天天在用,无非是看别人如何用,自己就如何用,但很少有人去总结一下,比如String 和StringBuffer这两个类有什么差别,应该有很多java程序员说不出来。另外Vector和HashMap这两个类大家都在用,有多少人能说清楚Vector和HashMap继承了哪些类,实现了哪些接口呢。

  1. 理论学习。认真阅读java API的以下内容:java.lang包中的String,StringBuffer,Number,Double,Float,Byte,Short,Integer,Long,Character,Boolean,Process,Runtime,System,Object,Cloneable,Class,ClassLoader,Thread,ThreadGroup,Runnable,Package类或接口。java.util包中的Collection,list,Set,ArrayList,LinkedList,Hashset,TreeSet,Iterator,Map,SortedMap,HashMap,TreeMap,Comparator,Arrays,Enumeration,Vector,Stack,Dictionary,Hashtable,Properties,StringTokenizer,BitSet,Date,Calendar,Locale,Random,Observer,Observable类和接口。主要理清楚这些类或接口的继承关系、主要功能。
  2. 演绎阶段。通过阅读代码,练习编程,学习并应用这些类和接口。其实这些类的大部分是经常用的,无非对这些类和接口说不清楚。在这些类中,对java.lang和java.util包中的类,最好将各个方法练习使用一次。而对java.util包中Collections framework中的类,最好理清楚各个类和接口之间的关系。常用的类的方法我就不具体介绍了,我举一个使用Observer和Obserable 的例子(摘自《The Complete Reference java 2 》)。在Observer和Obserable中,java实现了对设计模式Observer的支持。

             Watcher.java代码

    import java.util.*;

    class Watcher implements Observer
    {
     public void update(Observable obj, Object arg )
     {
      System.out.println("Update() called, count is " +
       ((Integer) arg).intValue());
     }
    }
          

          BeingWatched.java代码

    import java.util.*;

    class BeingWatched extends Observable
    {
     void counter( int period )
     {
      for(;period >=0; period-- )
      {
       setChanged();
       notifyObservers( new Integer ( period ) );
       try
       {
        Thread.sleep(100);
       }
       catch( InterruptedException e)
       {
        System.out.println("Sleep interrupeted" );
       }
      }
     }
    };

           ObserverDemo.java代码

    public class ObserverDemo
    {
     public static void main( String[] args )
     {
      BeingWatched observed = new BeingWatched();
      Watcher observing = new Watcher();
      observed.addObserver( observing);
      observed.counter(10);
     }
    };

    执行结果

    Update() called, count is 10
    Update() called, count is 9
    Update() called, count is 8
    Update() called, count is 7
    Update() called, count is 6
    Update() called, count is 5
    Update() called, count is 4
    Update() called, count is 3
    Update() called, count is 2
    Update() called, count is 1
    Update() called, count is 0

  3. 归纳阶段。总结使用经验,体会这两个包中类的继承关系,对设计模式的支持(如Iterator本身就是设计模式,同时也是工厂方法模式的应用),了解这些类提供的所有功能,比较功能相近类的异同。将这些类的api记在心中。

         虽然集成开发环境支持方法提示功能,但我个人认为,对于java.lang和java.util这两个包的api应当熟记于心,java的其他类库在使用时可以查找api。当然如果从事不同的开发,对其他类库应当有相应的了解,如开发swing,就应当对javax.swing包了如执掌,开发b/s结构的程序,就要了解javax.servlet,依个人的应用来开发了。

(未完)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值