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
*/
}
}
结果测试过的,对照理解。
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
*/
}
}
结果测试过的,对照理解。
相关文章推荐
- 具体的一个实例,展示如何访问实例变量和调用成员方法
- .NET(C#)中静态方法或实例方法中调用静态成员或者实例成员,有几种情况?
- 23对接收数组参数的成员方法进行反射24 数组与Object的关系及其反射类型25 数组的反射应用
- Java如何在静态方法中访问类的实例成员(没附加static关键字的字段或方法)
- Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的
- 为什么一个对象调用的类方法可以访问该类其他对象的私有成员
- Java 库的建立方法及其实例
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- C++继承中成员变量关系及访问方法的深入探究
- OC中继承子类对象调用方法机制 子类对象访问父类中的实例变量
- 实例成员变量不能在静态方法中访问
- linux上的文件管理类命令有哪些,常用的使用方法及其相关实例演示
- OC 继承子类对象调用方法机制 子类对象访问父类中的实例变量
- DataTable的成员方法及其数据成员的调用
- 再续:匿名内部类调用父类方法或访问父类成员
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- 再续:匿名内部类调用父类方法或访问父类成员
- jsp:synchronized方法控制对类成员变量的访问(实例)