这个例子得到String的类名,保存在className中
public class Test{
public static void main(String[] args) { String s1="<123<456";
String tmp = s1.getClass().toString();
String className = tmp.substring(tmp.lastIndexOf(".")+1);
System.out.println(className);
}
}
public class Test{
public static void main(String[] args) { String s1="<123<456";
String tmp = s1.getClass().toString();
String className = tmp.substring(tmp.lastIndexOf(".")+1);
System.out.println(className);
}
}
解决方案 »
- 出了几个面试题,用来面试毕业生,大家看看难度是否合适?
- 堆 栈 用java代码实现。
- 求教于高手
- 请问JAVA编程思想第四版怎么样?
- 应界毕业生,对于java的困惑,定散分
- 请问我的ORACLE的PROCEDURE是写在一个包(PACKAGE)里的,我用JAVA该怎么调用PACKAGE里的PROCEDURE吗
- 扫雷
- 测试
- 问: 什么是调节器,什么是访问器 。
- java中如何实现两个ftp服务器之间的文件传输
- 如何得到resultset结果集中的字段数阿
- 使用jdbc-odbc连接mysql.出现java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY!
import java.util.*;public class Test{
public static void main(String[] args) { String s1="<123<456";
String className = null;
String tmp = s1.getClass().getName();
if (tmp.indexOf(".") != -1)
className = tmp.substring(tmp.lastIndexOf(".")+1);
else
className = tmp; System.out.println(className);
}
}
public static void test(List s) {
System.out.println("Testing " +
s.getClass().getName() + ");
.....
}
public static void main(String[] args) {
test(new ArrayList());
}其中s.getClass().getName() 得到结果为java.util.ArrayList
我想做一个类。比如叫A.A里面有个方法,可以自动获得调用A的类的类名。
比如类B调用了A,A里面的方法就自动获得了B这个类名。
程序的大概意思是:
public class B{
public static void main(String[] args) { A a = new A();
a.xxx();
}
}
通过类A的方法xxx(),就自动获得了class B的类名称B.
不知道,我说清楚了没有。
请问大家,我该怎么做呢?
String name = A.getClassName(b);
public class A{
public static String getClassName(Object o){
String className = null;
String tmp = o.getClass().getName();
if (tmp.indexOf(".") != -1)
className = tmp.substring(tmp.lastIndexOf(".")+1);
else
className = tmp; return className;
}
public static void main(String[] args) { String s1="<123<456";
System.out.println(getClassName(s1));
}
}
public static String getClassName(Class c){
String className = null;
String tmp = c.getName();
if (tmp.indexOf(".") != -1)
className = tmp.substring(tmp.lastIndexOf(".")+1);
else
className = tmp; return className;
}
}
可以直接用getClass()吗?不需要实例化吗?
你说容易,那又该怎么做呢?请说清楚。