<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    zhyiwww
    用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
    posts - 536,comments - 394,trackbacks - 0
    今天在看
    http://m.tkk7.com/raylong1982/archive/2007/10/24/155439.html
    中關(guān)于內(nèi)部類的介紹和分析的時(shí)候,就想到了我剛寫的那個(gè)回答面試問題的簡單代碼,是不是也可以改用內(nèi)部類來實(shí)現(xiàn)呢?
    所以就做了一個(gè)簡單的改進(jìn),代碼如下:
    package org.zy.demo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class ParseUrlUsingInnerClass {
    ???
    ???
    ???
    ??? public static void main(String[] args){
    ??? ??? Timer t = new Timer();
    ??? ??? t.schedule(new ParseTask(),0,5*60*60);??? ??? ???
    ??? }
    ???
    ???
    ??? class ParseTask extends TimerTask{

    ??? ??? public void run(){
    ??? ??? ??? System.out.println("runing");
    ??? ??? ??? try {
    ??? ??? ??? ??? // create url object
    ??? ??? ??? ??? URL url = new URL("http://post.baidu.com/f?kw=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC");
    ??? ??? ??? ??? // get the input stream reader from the url
    ??? ??? ??? ??? InputStreamReader isr = new InputStreamReader(url.openStream());
    ??? ??? ??? ??? // buffered the reader
    ??? ??? ??? ??? BufferedReader br = new BufferedReader(isr);
    ??? ??? ??? ???
    ??? ??? ??? ??? // store the temp string
    ??? ??? ??? ??? StringBuffer sb = new StringBuffer(10000);
    ??? ??? ??? ??? // temporary variable for each read
    ??? ??? ??? ??? String tmp="";
    ??? ??? ??? ???
    ??? ??? ??? ??? // read the content from reader
    ??? ??? ??? ??? while((tmp=br.readLine())!=null){
    ??? ??? ??? ??? ??? sb.append(tmp);
    ??? ??? ??? ??? }
    ??? ??? ??? ??? System.out.println(sb.toString());
    ??? ??? ??? ??? // match from the orginal string using? reglex express
    ??? ??? ??? ??? Pattern p = Pattern.compile("<<.*異人.*>>");
    ??? ??? ??? ??? Matcher m = p.matcher(sb.toString());??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? } catch (MalformedURLException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? } catch (IOException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? }
    ??? ??? }
    ??? }
    }

    看藍(lán)色代碼部分,很顯然,我直接作了一個(gè)最簡單的修改,就是直接把外面定義的拿給類拿到了public類的里面,其他的代碼都沒有改變。
    但是代碼卻出現(xiàn)了問題。
    ??? public static void main(String[] args){
    ??? ??? Timer t = new Timer();
    ??? ??? t.schedule(new ParseTask(),0,5*60*60);??? ??? ???
    ??? }
    上面的這段代碼就出現(xiàn)了問題,為什么呢?
    我們知道,一個(gè)static的方法,只能使用static屬性和方法和static類,那么
    new ParseTask()能行嗎?
    問題就出在這,此處不能創(chuàng)建一個(gè)非靜態(tài)類的對(duì)象。
    所以,我們就只能在類的定義前面添加一個(gè)static,讓其成為一個(gè)靜態(tài)類就可以了。
    修改后的代碼如下:
    static class ParseTask extends TimerTask{
    一般情況下,我們不創(chuàng)建static類,這個(gè)地方是因?yàn)槲覀冊(cè)趕tatic main 方法里面直接使用,所以就只能創(chuàng)建靜態(tài)類。
    我們的代碼還可以改變:
    可以把調(diào)用部分封裝成一個(gè)方法,然后在此方法里面調(diào)用內(nèi)部類,就可以不用定義靜態(tài)類了,代碼如下:

    package org.zy.demo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class ParseUrlUsingInnerClass1 {
    ???
    ??? public void parseUrl(){
    ??? ??? Timer t = new Timer();
    ??? ??? t.schedule(new ParseTask(),0,5*60*60);??? ???
    ??? }
    ???
    ??? public static void main(String[] args){
    ??? ??? ParseUrlUsingInnerClass1 puic = new ParseUrlUsingInnerClass1();
    ??? ??? puic.parseUrl();
    ??? }
    ???
    ???
    ??? class ParseTask extends TimerTask{

    ??? ??? public void run(){
    ??? ??? ??? System.out.println("runing");
    ??? ??? ??? try {
    ??? ??? ??? ??? // create url object
    ??? ??? ??? ??? URL url = new URL("http://post.baidu.com/f?kw=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC");
    ??? ??? ??? ??? // get the input stream reader from the url
    ??? ??? ??? ??? InputStreamReader isr = new InputStreamReader(url.openStream());
    ??? ??? ??? ??? // buffered the reader
    ??? ??? ??? ??? BufferedReader br = new BufferedReader(isr);
    ??? ??? ??? ???
    ??? ??? ??? ??? // store the temp string
    ??? ??? ??? ??? StringBuffer sb = new StringBuffer(10000);
    ??? ??? ??? ??? // temporary variable for each read
    ??? ??? ??? ??? String tmp="";
    ??? ??? ??? ???
    ??? ??? ??? ??? // read the content from reader
    ??? ??? ??? ??? while((tmp=br.readLine())!=null){
    ??? ??? ??? ??? ??? sb.append(tmp);
    ??? ??? ??? ??? }
    ??? ??? ??? ??? System.out.println(sb.toString());
    ??? ??? ??? ??? // match from the orginal string using? reglex express
    ??? ??? ??? ??? Pattern p = Pattern.compile("<<.*異人.*>>");
    ??? ??? ??? ??? Matcher m = p.matcher(sb.toString());??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? } catch (MalformedURLException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? } catch (IOException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? }
    ??? ??? }
    ??? }
    }
    看綠色代碼部分就可以知道了。
    對(duì)于方法的改進(jìn),我們可以給parseUrl加上一個(gè)參數(shù)URL,那么,其他的相對(duì)應(yīng)的部分也要做一些簡單的修改,代碼如下:
    package org.zy.demo;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class ParseUrlUsingInnerClass2 {
    ???
    ??? public void parseUrl(URL url){
    ??? ??? Timer t = new Timer();
    ??? ??? t.schedule(new ParseTask(url),0,5*60*60);??? ???
    ??? }

    ???
    ??? public static void main(String[] args){
    ??? ??? ParseUrlUsingInnerClass2 puic = new ParseUrlUsingInnerClass2();
    ??? ???
    ??? ??? try {
    ??? ??? ??
    URL url = new URL("http://post.baidu.com/f?kw=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC");
    ??? ??? ??? puic.parseUrl(url);

    ??? ??? } catch (MalformedURLException e) {
    ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? e.printStackTrace();
    ??? ??? }
    ???
    ??? }
    ???
    ???
    ??? class ParseTask extends TimerTask{
    ??? ??? URL url;
    ??? ??? public ParseTask(URL url){
    ??? ??? ??? this.url = url;
    ??? ??? }

    ??? ??? public void run(){
    ??? ??? ??? System.out.println("runing");
    ??? ??? ??? try {
    ??? ??? ??? ??? // create url object
    ??? ??? ??? ??? //URL url = new URL("http://post.baidu.com/f?kw=%D2%EC%C8%CB%B0%C1%CA%C0%C2%BC");
    ??? ??? ??? ??? // get the input stream reader from the url
    ??? ??? ??? ??? InputStreamReader isr = new InputStreamReader(this.url.openStream());
    ??? ??? ??? ??? // buffered the reader
    ??? ??? ??? ??? BufferedReader br = new BufferedReader(isr);
    ??? ??? ??? ???
    ??? ??? ??? ??? // store the temp string
    ??? ??? ??? ??? StringBuffer sb = new StringBuffer(10000);
    ??? ??? ??? ??? // temporary variable for each read
    ??? ??? ??? ??? String tmp="";
    ??? ??? ??? ???
    ??? ??? ??? ??? // read the content from reader
    ??? ??? ??? ??? while((tmp=br.readLine())!=null){
    ??? ??? ??? ??? ??? sb.append(tmp);
    ??? ??? ??? ??? }
    ??? ??? ??? ??? System.out.println(sb.toString());
    ??? ??? ??? ??? // match from the orginal string using? reglex express
    ??? ??? ??? ??? Pattern p = Pattern.compile("<<.*異人.*>>");
    ??? ??? ??? ??? Matcher m = p.matcher(sb.toString());??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? ???
    ??? ??? ??? } catch (MalformedURLException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? } catch (IOException e) {
    ??? ??? ??? ??? // TODO Auto-generated catch block
    ??? ??? ??? ??? e.printStackTrace();
    ??? ??? ??? }
    ??? ??? }
    ??? }
    }

    上面在用到此URL的地方和參數(shù)傳遞的地方都要做相應(yīng)的修改,比如說
    ParseTask類的構(gòu)造器,都要做些修改。
    上面的類的定義和方法的定義,已經(jīng)可以實(shí)現(xiàn)減少部分耦合了。其實(shí)在run方法里面,如果從代碼重用的角度來看的話,那么我們還可以進(jìn)行代碼改進(jìn)和重構(gòu)。
    就上面的一個(gè)demo,我們也可以簡單的看到在類的設(shè)計(jì)中,我們還是有很多的問題要考慮,還是有很多的細(xì)節(jié)可以注意,來提高我們的代碼質(zhì)量。
    以上僅是一點(diǎn)簡單的理解。






    |----------------------------------------------------------------------------------------|
                               版權(quán)聲明  版權(quán)所有 @zhyiwww
                引用請(qǐng)注明來源 http://m.tkk7.com/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2007-10-25 11:19 zhyiwww 閱讀(907) 評(píng)論(1)  編輯  收藏 所屬分類: java basic

    FeedBack:
    # re: 從類的改造小談內(nèi)部類
    2009-11-11 19:03 | s
    發(fā)表任何與政治相關(guān)的內(nèi)容  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: A级毛片高清免费视频在线播放| 一进一出60分钟免费视频| 大地资源在线资源免费观看| 亚洲国产精品自产在线播放 | 成人免费一级毛片在线播放视频| 亚洲人成电影在线天堂| 午夜影院免费观看| 亚洲精品白色在线发布| xxxxx免费视频| 亚洲日韩国产二区无码 | 美女黄网站人色视频免费国产 | 亚洲第一综合天堂另类专| 成全影视免费观看大全二| 亚洲av无码成人影院一区| 亚洲A∨精品一区二区三区| 免费无码婬片aaa直播表情| 美女尿口扒开图片免费| 可以免费观看一级毛片黄a | 亚洲Av无码乱码在线znlu| 无码人妻一区二区三区免费视频 | 亚洲一区在线免费观看| 成人啪精品视频免费网站| 免费一区二区三区在线视频| 国产亚洲精品久久久久秋霞| 久久香蕉国产线看免费| 色老板亚洲视频免在线观| 亚洲国产成人久久综合碰| 国产免费拔擦拔擦8X高清在线人| 久久亚洲sm情趣捆绑调教| 卡一卡二卡三在线入口免费| 成人免费夜片在线观看| 亚洲色图视频在线观看| 日韩成人免费在线| 免费毛片在线看不用播放器| 国产.亚洲.欧洲在线| 亚洲?V无码成人精品区日韩| 无码A级毛片免费视频内谢| 亚洲日韩国产欧美一区二区三区 | 日韩免费高清播放器| 亚洲三级视频在线| 亚洲午夜久久久影院|