有一web系统,有深圳,广州及其它地区的用户
不同地区的用户显示的页面基本相同;为了方便管理,考虑用同一个servlet,
在servlet中,我想用方法覆写技术,根据地区的不同执行不同的程序,
问题:能否在方法中使用常量,例如
ListPage("shengzhen",HttpServletRequest request, HttpServletResponse response){}
ListPage("guangzhou",HttpServletRequest request, HttpServletResponse response){}
不同地区的用户显示的页面基本相同;为了方便管理,考虑用同一个servlet,
在servlet中,我想用方法覆写技术,根据地区的不同执行不同的程序,
问题:能否在方法中使用常量,例如
ListPage("shengzhen",HttpServletRequest request, HttpServletResponse response){}
ListPage("guangzhou",HttpServletRequest request, HttpServletResponse response){}
api不是很多方法都有常量参数的
举个简单的例子就是JOptionPane.showMessageDialog(),楼主去查一下api,有些参数就是常量,指定显示的图片
ListPage(String city,HttpServletRequest request, HttpServletResponse response){}然后在类中定义两个常量
public static final String CITY_GUANGZHOU="guangzhou";
public static final String CITY_SHENZHEN="shengzhen";调用就行了
按你所说
public static final String CITY_GUANGZHOU="guangzhou";
public static final String CITY_SHENZHEN="shengzhen";ListPage(String CITY_GUANGZHOU,HttpServletRequest request, HttpServletResponse response){}
ListPage(String CITY_SHENZHEN,HttpServletRequest request, HttpServletResponse response){}错误提示:方法ListPage(String,HttpServletRequest,HttpServletResponse)重复
......
}其中第一个参数是常量!既然是常量,传与不传,有什么意义呢?所以,我认为java是不会让人这么干的,其他语言也同样不会让人这么干的。
ListPage(String CITY_GUANGZHOU,HttpServletRequest request, HttpServletResponse response){}
ListPage(String CITY_SHENZHEN,HttpServletRequest request, HttpServletResponse response){}而是让你写成:
ListPage(String city,HttpServletRequest request, HttpServletResponse response){}调用的时候:
ListPage(CITY_GUANGZHOU, request, response);
ListPage(CITY_SHENZHEN, request, response);
谢谢大家的回复:参数可以是常量问题已明确了不过有关方法覆写的问题还在:根据用户地区的不同执行不同的程序,为了方便管理,我不想在程序里有大量的对用户地区判断的if..else.希望是用多个方法,名称相同,只是地区的参数不同,即覆写技术来解决这个问题.
{
if( str.equalsIgnoreCase("shenzhen") )
ListPageShenZhen( request...);
else if( str.equalsIgoreCase("guangzhou") )
ListPageGuangZhou( request...);
}
=========================================??????
明确了?明确什么了?写成这样就行了?public static final String CITY_GUANGZHOU="guangzhou";
public static final String CITY_SHENZHEN="shengzhen";public static void ListPage(String CITY_GUANGZHOU){
System.out.println(CITY_GUANGZHOU);
}不对吧?如果这样调用会是什么样子?
ListPage(CITY_SHENZHEN,a,b);
ListPage(CITY_GUANGZHOU,a,b);是这个样子!!
shengzhen
guangzhou
}
//这里的CITY_GUANGZHOU和上面一句里的CITY_GUANGZHOU不是一码事!!! System.out.println(CITY_GUANGZHOU);
}