getClass 与getSimpleName

//首先定义一个借口
package com.test;

public interface Fruit {

}
//定义一个实现类


package com.test;

public class Apple implements Fruit {

}

//定义实现类进行测试
package com.test;

import java.util.ArrayList;
import java.util.List;

public class TestName {
    public static void main(String[] args) {
        Fruit apple=new Apple();
        System.out.println(apple.getClass().getCanonicalName());//返回com.test.Apple
        System.out.println(apple.getClass().getSimpleName());//Apple
        System.out.println(apple.getClass().getName());//返回com.test.Apple
        
        Apple[] arrApple=new Apple[]{};
        System.out.println(arrApple.getClass().getCanonicalName());//返回com.test.Apple[]
        System.out.println(arrApple.getClass().getSimpleName());//返回Apple[]
        System.out.println(arrApple.getClass().getName());//返回[Lcom.test.Apple;
        
        System.out.println(String.class.getCanonicalName());//返回java.lang.String
        System.out.println(String.class.getSimpleName());//返回String
        System.out.println(String.class.getName());//返回java.lang.String
        
        System.out.println(int.class.getCanonicalName());//返回int
        System.out.println(int.class.getSimpleName());//返回int
        System.out.println(int.class.getName());//返回int
        
        Apple a1=new Apple();
        Apple a2=new Apple();
        List<Apple> appleList=new ArrayList<Apple>();
        appleList.add(a1);
        appleList.add(a2);
        System.out.println(appleList.getClass().getCanonicalName());//返回java.util.ArrayList
        System.out.println(appleList.getClass().getSimpleName());//返回ArrayList
        System.out.println(appleList.getClass().getName());//返回java.util.ArrayList
        
    }
}


//实际应用,运用泛型
public <T> List<T> getRecords(Class<T> c,Date startDate,Date endDate){
        StringBuilder hql = new StringBuilder("select t from ");
        hql.append(c.getCanonicalName());
        hql.append(" t where t.statTime>=:startTime and t.statTime<:endTime ");

        Query query = sessionFactory.getCurrentSession().createQuery(hql.toString());
        query.setParameter("startTime", startDate);
        query.setParameter("endTime", endDate);
        
        return query.list();
    }
}
Class类,是获取类的类模板实例对象,通过反射的机制获取。
根据API中的定义,Class.getSimpleName()方法。是获取源代码中给出的‘底层类’简称
而Class.getName();以String的形式,返回Class对象的‘实体’名称
 
参考:http://sunyimaying0925-gmail-com.iteye.com/blog/768789
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Activity tempActivity = App.getMultiRoundActivity(); if (tempActivity != null && tempActivity instanceof MapBaseActivity) { ((MapBaseActivity) tempActivity).searchPoiWithLocationCheck(pi); }这个调用方法是, public void searchPoiWithLocationCheck(PoiSearchInfo poiSearchInfo) { this.mPoiSearchInfo = poiSearchInfo; Log.i(getClass().getSimpleName(), "searchPoiWithLocationCheck: "); mHandler.post(() -> { if (location_last_success_time == 0 || (System.currentTimeMillis() - location_last_success_time) > LOCATION_MAX_USE_TIME) { Log.i(MapBaseActivity.this.getClass().getSimpleName(), "searchPoiWithLocationCheck , wait for location ready" + mLocClient); if (mToastLocating == null) { mToastLocating = Toast.makeText(MapBaseActivity.this, "正在定位,请稍后", Toast.LENGTH_LONG); } if (mLocClient == null) { Log.i(MapBaseActivity.this.getClass().getSimpleName(), "searchPoiWithLocationCheck : reInit location caused by null"); initLocation(); } else if (!mLocClient.isStarted()) { Log.i(MapBaseActivity.this.getClass().getSimpleName(), "searchPoiWithLocationCheck : restart location caused by stoped"); mLocClient.start(); } mIsPoiSearchNeeded = true; mToastLocating.show(); } else { searchPoi(poiSearchInfo); } }); 其中,会走else,也就是searchPoi(poiSearchInfo);方法, public void searchPoi(PoiSearchInfo poiSearchInfo) { Log.i(getClass().getSimpleName(), "searchPoi: " + poiSearchInfo ); mIsPoiSearchNeeded = false; // 配置请求参数 附近检索 if (poiSearchInfo.getSearchType() == Constant.MAP_SEARCH_CITY){ searchInCity(poiSearchInfo); } else { searchNearBy(poiSearchInfo); } } 帮我修改一下这个调用方法的时候,要区分是哪一个行动,也就是去修改去目的地还是添加途经点
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值