经过看书的困难,今天重新开始总结知识点:
1、Instanceof和getClass的区别
在是这样描述他们的区别的:instanceof保持着类型的概念,它指的是“你是这个类吗?”或者“你是这个类的派生类吗?”,而getClass得到的是确切的类型,并不考虑继承,它判断的是引用指向的对象的类型,与声明该变量的类型无关。
//测试代码如下:
import static net.mindview.util.Print.*;
class Base{}
class Derived extends Base{}
public class FamilyVsExactType {
static void test(Object x)
{
print("Tesing x of type " + x.getClass());
print("x instanceof Base " + (x instanceof Base));
print("x instanceof Derived " + (x instanceof Derived));
print("Base.isInstance(x) " + Base.class.isInstance(x));
print("Derived.isInstance(x) " + Derived.class.isInstance(x));
print("x.getClass() == Base.class " + (x.getClass()==Base.class));
print("x.getclass() == Derived.class " + (x.getClass() == Derived.class));
print("x.getclass().equals(Base.class) " + (x.getClass().equals(Base.class)));
print("x.getclass().equals(Derived.class) " + (x.getClass().equals(Derived.class)));
}
public static void main(String[] args)
{
test(new Base());
test(new Derived());
}
}
//测试结果:
x instanceof Base true
x instanceof Derived false
Base.isInstance(x) true
Derived.isInstance(x) false
x.getClass() == Base.class true
x.getclass() == Derived.class false
x.getclass().equals(Base.class) true
x.getclass().equals(Derived.class) false
Tesing x of type class typeinfo.Derived
x instanceof Base true
x instanceof Derived true
Base.isInstance(x) true
Derived.isInstance(x) true
x.getClass() == Base.class false
x.getclass() == Derived.class true
x.getclass().equals(Base.class) false
x.getclass().equals(Derived.class) true
2、初步接触反射
反射是指程序可以访问,检测甚至修改自身状态或者行为的一种机制。Class类和java.lang.reflect类库都对反射的概念进行了支持,这种类库包括了Field、Method以及Constructor类(每一个类都实现了Member接口),这些类型的对象是由JVM在运行时创建的。通常情况下不需要直接使用反射工具,但是当在需要创建动态代码时会很有用。根据编程思想的例子类方法提取器,可以稍微了解反射的优点:
//类方法提取器
package typeinfo;
import java.lang.reflect.*;
import java.util.regex.*;
import static net.mindview.util.Print.*;
public class ShowMethods {
private static String usage =
"usage:\n" +
"ShowMethods qualified.class.name\n"
+ "To show all methods in class or:\n" +
"ShowMethods qualified .class.name word\n" +
"To search for methods involving 'word'";
private static Pattern p = Pattern.compile("\\w+\\.");
public static void main(String[] args)
{
if(args.length < 1)
{
print(usage);
System.exit(0);
}
int lines = 0;
try{
Class<?> c = Class.forName(args[0]);
Method[] methods = c.getMethods();
Constructor[] ctors = c.getConstructors();
if(args.length == 1)
{
for(Method method : methods)
{
print(
p.matcher(method.toString()).replaceAll("")
);
}
for(Constructor ctor : ctors)
print(p.matcher(ctor.toString()).replaceAll(""));
lines = methods.length + ctors.length;
}
else
{
for(Method method : methods)
if(method.toString().indexOf(args[1]) != -1)
{
print(p.matcher(method.toString()).replaceAll(""));
lines++;
}
for(Constructor ctor : ctors)
if(ctor.toString().indexOf(args[1]) != -1)
{
print(p.matcher(ctor.toString()).replaceAll(""));
lines++;
}
}
}
catch(ClassNotFoundException e)
{
print("No such class: " + e);
}
}
}
//运行结果
public static void main(String[])
public final void wait() throws InterruptedException
public final void wait(long,int) throws InterruptedException
public final native void wait(long) throws InterruptedException
public boolean equals(Object)
public String toString()
public native int hashCode()
public final native Class getClass()
public final native void notify()
public final native void notifyAll()
public ShowMethods()
从运行结果可以看出,Class的getMethods()和getConstructors()方法分别反悔了Method对象的数组和Constructor对象的数组。其中Class.forName()的生成结果在编译是不可知的,因此所有的方法特征签名信息都是在执行时被提取出来的。