您的位置:首页 > 其它

Abstract Class的建立类及其成员及方法的调用关系访问等的实例演示

2008-04-28 21:03 423 查看
首先说明:程序没有内在逻辑关系,仅仅用来演示类及其成员及方法的调用关系:

1 基类




/**//**


 * 


 */


package com;






/**//**


 * @author rulinma


 *


 */




public abstract class WebSpide ...{






    /**//** test for static and public */


    public static boolean debugEnabled = false;






    /**//** test for static and private */


    private static int threadCount = 0;






    /**//** test for static and protected */


    protected static  float countMoney = 0.0f;


    




    /**//** test for static and no class declare */


    static String threadName = "";


    


    public int getIncreament()




    ...{


        return threadCount;


    }


    


    public int setIncreament(int inc)




    ...{


        return threadCount = inc;


    }


    


    public boolean getDebug()




    ...{


        return debugEnabled;


    }


    


    abstract public String getThreadName();


    


    public final static void outPrint()




    ...{


        System.out.println("this is public final static test");


    }


    


    public final static void outPrint(String str)




    ...{


        System.out.println(str);


    }


    


    public final void outPrint(String str, String another)




    ...{


        System.out.println(str);


        System.out.println(another);


    }


    


}



2 抽象类的继承




/**//**


 * 


 */


package com;






/**//**


 * @author rulinma


 *


 */




public abstract class HttpWebSpide extends WebSpide ...{


    


    private String httpConn = "";


    


    public String database = "";


    


    abstract public String getThreadName();


    


    public void getInterActive(String temp)




    ...{


        System.out.println(temp);


    }


    


    public void setHttpConn(String httpConnStr)




    ...{


        httpConn = httpConnStr;


    }


    


    public String getHttpConn()




    ...{


        return httpConn;


    }


    


}



3 实类继承之一


package com;






public class HttpBrowse extends HttpWebSpide ...{




    public static int timeOut = 10;


    


    @Override




    public String getThreadName() ...{


        System.out.println("http getThreadName");


        return null;


    }






    public void runOne() ...{


        System.out.println("httpBrowse runOne");




        // call the ancentor's method


        setHttpConn("hhhhhhhh");


        outPrint();


    }


    


    


    


    




}





4 实类继承之二


package com;






public class FtpDownLoad extends HttpWebSpide ...{




    public static int timeOut = 20;


    


    @Override




    public String getThreadName() ...{


        System.out.println("ftp get thread name");


        return null;


    }




}



5 测试类
同一包和不同包的访问,自己测试,主要就是默认和protected的访问。

package test;

import com.FtpDownLoad;
import com.HttpBrowse;
import com.HttpWebSpide;
import com.WebSpide;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
        //System.out.println("test is beginging...");
       
        //staticVariableTest();
       
        //staticVariableSingleClass();
       
        //classVariable();
       
        //methodDebug();
       
        //classCall();
       
        //subClassCall();
       
        //System.out.println("test is end");
    }

    /**
     * class static variable test
     */
    public static void staticVariableTest()
    {
        HttpBrowse http = new HttpBrowse();
        http.debugEnabled = true;
        System.out.println(http.debugEnabled);
       
        FtpDownLoad ftp = new FtpDownLoad();
        System.out.println(ftp.debugEnabled);
        ftp.debugEnabled = false;
        System.out.println(http.debugEnabled);
       
        WebSpide.debugEnabled = true;
        System.out.println(http.debugEnabled);
        System.out.println(ftp.debugEnabled);
       
        /* result:
                    true
                    true
                    false
                    true
                    true
        */
    }

    /**
     * single class static variable test
     */
    public static void staticVariableSingleClass()
    {
        HttpBrowse http = new HttpBrowse();
       
        System.out.println(http.timeOut);
        http.timeOut = 20;
        System.out.println(http.timeOut);

        HttpBrowse httpAnother = new HttpBrowse();
        System.out.println(httpAnother.timeOut);
        httpAnother.timeOut = 30;
        System.out.println(http.timeOut);
        System.out.println(httpAnother.timeOut);
       
        /* result:
                     10
                    20
                    20
                    30
                    30
         */
    }

    /**
     * class variable test
     */
    public static void classVariable()
    {
        // httpBrowse http = new httpBrowse();
        HttpWebSpide http = new HttpBrowse();
        http.database = "http database";
        System.out.println(http.database);
       
        // ftpDownLoad ftp = new ftpDownLoad();
        HttpWebSpide ftp = new FtpDownLoad();
        ftp.database = "ftp databse";
        System.out.println(ftp.database);

        System.out.println(http.database);
       
        /*
            http database
            ftp databse
            http database
         */
    }

    /**
     * method debug test
     */
    public static void methodDebug()
    {
        HttpWebSpide http = new HttpBrowse();
        //httpBrowse http = new httpBrowse();
       
        http.setHttpConn("this is a http conn str test");
        System.out.println(http.getHttpConn());
        http.setHttpConn("this is another http conn str test");
        System.out.println(http.getHttpConn());
       
        HttpWebSpide ftp = new FtpDownLoad();
        //ftpDownLoad ftp = new ftpDownLoad();
       
        ftp.setHttpConn("this is a ftpconn str test");
        System.out.println(ftp.getHttpConn());
        ftp.setHttpConn("this is another ftp conn str test");
        System.out.println(ftp.getHttpConn());
       
        System.out.println(http.getHttpConn());
       
        /* result:
                    this is a http conn str test
                    this is another http conn str test
                    this is a ftpconn str test
                    this is another ftp conn str test
                    this is another http conn str test
         */
    }

    /**
     * class called test
     */
    private static void classCall()
    {
        WebSpide.outPrint();
        WebSpide.outPrint("this is a test");
       
        //WebSpide http = new httpBrowse();
        HttpWebSpide http = new HttpBrowse();
        http.outPrint("one-one ", "two-two");
        http.getThreadName();
        http.getHttpConn();

        System.out.println(http.getIncreament());
        http.setIncreament(3);
        System.out.println(http.getIncreament());

        HttpWebSpide ftp = new FtpDownLoad();
        ftp.getThreadName();
       
        //http.runOne(); // can not pass compile ,need httpBrowse http = new httpBrowse();
       
        /*
         *  this is public final static test
            this is a test
            one-one
            two-two
            http getThreadName
            0
            3
            ftp get thread name
         */
       
    }

    /**
     * subclass call ancentor's method
     */
    private static void subClassCall(){
        HttpBrowse http = new HttpBrowse();
        http.runOne();
        System.out.println(http.getHttpConn());
       
        /*
            httpBrowse runOne
            this is public final static test
            hhhhhhhh
         */
    }
   
}

结果测试过的,对照理解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐