首页 > 代码库 > java static 方法使用笔记
java static 方法使用笔记
有入参的static方法,可以正常使用
static的作用是申明:这是类的静态方法,什么时候都可以调用,可以传入入参,也可以不传。
上代码:
1.带静态方法的类:
public class MakeParameters { public static InsuranceSearchRQ makeBaseRqParms(InsuranceSearchRQ rq){ ChannelInfo channel = new ChannelInfo(); channel.setFirstChannelNo(BaseConst.FIRST_CHANNEL_NO); channel.setWebSite(BaseConst.WEB_SITE); rq.setChannelInfo(channel); rq.setLanguage(BaseConst.LANGUAG); rq.setTimeStamp(BaseConst.TIMESTAMP); rq.setTransactionId(BaseConst.TRANSACTION_ID); rq.setVersion(BaseConst.VERSION); return rq; }}
2.测试类:
package com.doit.zz;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.basemake.zz.MakeParameters;import com.travelsky.logic.insurance.dto.InsuranceSearchRQ;import com.travelsky.logic.insurance.dto.InsuranceSearchRS;import com.travelsky.logic.insurance.dto.MaintainQueryParam;import com.travelsky.logic.shopping.service.IInsuranceSearchService;import com.utils.zz.JsonMapper;public class InsureTest { ApplicationContext ctx=new ClassPathXmlApplicationContext("helloservice.xml"); @Test public void testInsure() throws Exception{ InsuranceSearchRQ rq=new InsuranceSearchRQ(); MaintainQueryParam maintainQueryParam=new MaintainQueryParam(); maintainQueryParam.setProductCode("7000000"); rq.setMaintainQueryParam(maintainQueryParam); MakeParameters.makeBaseRqParms(rq); System.out.println(JsonMapper.toNonNullJson(rq));//1 maintainQueryParam.setProductCode("12345"); InsuranceSearchRQ rq2=new InsuranceSearchRQ(); rq2.setMaintainQueryParam(maintainQueryParam); MakeParameters.makeBaseRqParms(rq2); System.out.println(JsonMapper.toNonNullJson(rq2));//2 InsuranceSearchRS rs=new InsuranceSearchRS(); IInsuranceSearchService insService=(IInsuranceSearchService)ctx.getBean("iInsuranceSearchService"); rs=insService.getInsuranceSearchRS(rq); String rsJson = JsonMapper.toNonNullJson(rs); System.out.println(rsJson); } }
3.结果
{"channelInfo":{"firstChannelNo":"71","webSite":"zh_CN"},"timeStamp":"20150108161025","version":"0.01","transactionId":"20150108161025","language":"zh","insTravelerInfos":[],"maintainQueryParam":{"productCode":"7000000"}}
{"channelInfo":{"firstChannelNo":"71","webSite":"zh_CN"},"timeStamp":"20150108161025","version":"0.01","transactionId":"20150108161025","language":"zh","insTravelerInfos":[],"maintainQueryParam":{"productCode":"12345"}}
在1处和2出分别打印调用静态方法结果
可以看到:两次调用静态方法:入参不一样,结果也是不一样的。
java static 方法使用笔记
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。