ClassUtils常用方法总结,java实施工程师面试

2021/11/18 9:09:41

本文主要是介绍ClassUtils常用方法总结,java实施工程师面试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//获取canonicalName

String canonicalName = ClassUtils.getShortCanonicalName(String.class);

System.out.println(canonicalName); // = String

//获取className

String className = ClassUtils.getShortClassName(String.class);

System.out.println(className); // = String

//3者的区别:对于内部类,如内部类,区别就显示出来类

//其他情况区别不大

//获取simpleName

String simpleName3 = ClassUtils.getSimpleName(Map.Entry.class);

System.out.println(simpleName3); // = Entry

//获取canonicalName

String canonicalName2 = ClassUtils.getShortCanonicalName(Map.Entry.class);

System.out.println(canonicalName2); // = Map.Entry

//获取className

String className2 = ClassUtils.getShortClassName(Map.Entry.class);

System.out.println(className2); // = Map.Entry

}

判断是否为转型

isAssignable(Class<?> cls, Class<?> toClass)等等

@Test

public void testIsAssignable(){

System.out.println(“判断是否可以转型.”);

System.out.println(ClassUtils.isAssignable(Date.class, Object.class)); //= true

System.out.println(ClassUtils.isAssignable(Object.class, Date.class)); //=false

}

判断是否为内部类

isInnerClass(Class<?> cls)

@Test

public void testIsInnerClass(){

System.out.println(ClassUtils.isInnerClass(Entry.class)); // = ture

System.out.println(ClassUtils.isInnerClass(Object.class)); //= false

}

判读是否为基础类型或者包装类型

isPrimitiveOrWrapper(Class<?> type)

isPrimitiveWrapper(Class<?> type)

@Test

public void testIsPrimitiveWrapper(){

// wrapper类有(Boolean, Byte, Character, Short, Integer, Long, Double, Float).

System.out.println(ClassUtils.isPrimitiveWrapper(Integer.class)); // = ture

System.out.println(ClassUtils.isPrimitiveWrapper(Object.class)); // = false

System.out.println(ClassUtils.isPrimitiveWrapper(String.class)); // = false

System.out.println(ClassUtils.isPrimitiveWrapper(int.class)); // = false

System.out.println(ClassUtils.isPrimitiveOrWrapper(Integer.class)); // = ture

System.out.println(ClassUtils.isPrimitiveOrWrapper(Object.class)); // = false

System.out.println(ClassUtils.isPrimitiveOrWrapper(String.class)); // = false

System.out.println(ClassUtils.isPrimitiveOrWrapper(int.class)); // = true

}

基础类型和包装类型转换

**wrapperToPrimitive

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

开源分享完整内容戳这里

(Class<?> cls)**

@Test

public void testWrapperToPrimitive(){

System.out.println(ClassUtils.wrapperToPrimitive(Integer.class));// = int

}

获取类名和class类互转

convertClassesToClassNames(List<Class<?>> classes)

convertClassNamesToClasses(List classNames)

对象转Class对象

toClass(Object… array)

@Test

public void testToClass(){

//类转化为class对象

Class[] classAry = ClassUtils.toClass(new String(),new Object());

System.out.println(ArrayUtils.toString(classAry));

// = {class java.lang.String,class java.lang.Object}

}

获取缩略名

getAbbreviatedName(Class<?> cls,int len)

getAbbreviatedName(String className,int len)

@Test

public void testGetAbbreviatedName(){

//获取类的缩略名称

String abbreviatedName1 = ClassUtils.getAbbreviatedName(String.class, 5);

String abbreviatedName2 = ClassUtils.getAbbreviatedName(String.class, 10);

String abbreviatedName3 = ClassUtils.getAbbreviatedName(String.class, 3);

String abbreviatedName4 = ClassUtils.getAbbreviatedName(String.class, 20);

System.out.println(abbreviatedName1);

System.out.println(abbreviatedName2);

System.out.println(abbreviatedName3);

System.out.println(abbreviatedName4);

}

获得所有实现的接口

getAllInterfaces(Class<?> cls)

@Test

public void testGetAllInterfaces(){

//获得所有实现的接口

List<Class<?>> classAry = ClassUtils.getAllInterfaces(String.class);

System.out.println(ArrayUtils.toString(classAry));

}

获取所有父类

getAllSuperclasses(Class<?> cls)

@Test

public void testGetAllSuperclasses(){

//获得所有的父类

List<Class<?>> classArray = ClassUtils.getAllSuperclasses(String.class);

System.out.println(ArrayUtils.toString(classArray));

//[class java.lang.Object]

}



这篇关于ClassUtils常用方法总结,java实施工程师面试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程