class A { B b = new B(); public B getB() { return b; } }class B { C c = new C(); public C getC() { return c; } }class C { } === C c = new A().getB().getC();
加个括号看嘛 ( ( ( A.getB() ).getC() ) .getD() );
诸如get.get.get(),相当于你对某人喊:叫你的儿子的儿子的儿子去干工。
jingweixml(可扩展标记时代)形容的实在太形象了 就是大家说的那样
public class MyTest { public void func() { System.out.println("func"); }
C c = b.getC();
D d = c.getD();
{
B b = new B();
public B getB()
{
return b;
}
}class B
{
C c = new C();
public C getC()
{
return c;
}
}class C
{
}
===
C c = new A().getB().getC();
就是大家说的那样
{
public void func()
{
System.out.println("func");
}
public static void main(String[] args)
{
Object obj=new MyTest();
try
{/*
//下面这行可以成功编译
((MyTest)obj).getClass().newInstance().func();*/
/*下面这两行无法通过编译
Class c=((MyTest)obj).getClass();
//如果把此行改为Class<? extends MyTest>就可以编译通过了。
c.newInstance().func();*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
}/*是因为((MyTest)obj).getClass()返回的为对象类型为Class<? extends MyTest>,但是我们
第一行将结果强制转换成了Class,然后再去调用Class的newInstance方法,而不是去调用
Class<MyTest>的newInstance方法,编译器当然不再认为Class的newInstance方法返回的对象为MyTest了.*/
set
get
set