汗,楼主getClass().getInterfaces[0].getName()能编译通过??
都不可能编译通过
正确的代码是:
System.out.println("interface name is: " + o.getClass().getInterfaces()[0].getName());
System.out.println("interface name is: " + i2.getClass().getInterfaces()[0].getName());
public class IAmNewerImpl implements IAmNewer {
private String name = "hello";
public IAmNewerImpl () {
super();
}
public String getName()
{
return name;
}
}
//main\Test1.java
package main;
import main.*;
public class Test1 {
public static void main(String[] args)
{
IAmNewer i = new IAmNewerImpl();
Object o = i;
if (o.getClass().isInstance(i))
System.out.println("yes, o is a IAmNewer.");
String name = o.getClass().getSuperclass().getName();
System.out.println("class name is: " + name);
System.out.println("interface name is: " + o.getClass().getInterfaces[0].getName()); //OK
IAmNewer i2 = (IAmNewer) o;
System.out.println("the name is: " + i2.getName());
System.out.println("class name is: " + i2.getClass().getName());
System.out.println("interface name is: " + i2.getClass().getInterfaces[0].getName()); //failured???
/*
compiler error: symbol not found "getInterfaces"
*/
}
}